Python Forum
[DOS/cmd] How to handle accents?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[DOS/cmd] How to handle accents?
#1
Question 
Hello,

On Windows, I need to write a Python script to build a command and save it into a batch file (ie. cmd) that calls a command-line.

Problem is, some files have accents in them, and I can't figure out what encoding to use to get it to work:

import sys import os import glob APP = r"C:\temp\myCLI.exe" OUTPUT = "output.gpx" line = f'"{APP}" -i gpx ' for item in glob.glob("*.gpx"):	line += f'-f "{item}"' whole = f"{line} -o gpx -F {OUTPUT}" print(whole) #BAD with open(f"{OUTPUT}.cmd", "w",encoding='utf-8') as file: #BAD with open(f"{OUTPUT}.cmd", "w") as file: #BAD with open(f"{OUTPUT}.cmd", "w", encoding='cp1252') as file: with open(f"{OUTPUT}.cmd", "w", encoding='cp850') as file:	file.write(whole) #Running .cmd batch script: Cannot open 'blah.gpx' for read. Error was 'The system cannot find the file specified.'.
Thank you.
Reply
#2
glob will return just the file names. So I guess CWD from which you execute the batch file is different from the one where the location of the files are. i.e. the problem is not with the encoding, but missing/wrong path. Also, why cp850 instead of utf-8 when write to the batch file?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Nope: The script, the files, and the .cmd are all in the same directory. The error "The system cannot find the file specified." goes away if I rename the files with no accents eg. 1.gpx, 2.gpx.

I tried different encodings because, even in 2025, I'm no sure batch scripts on Windows support utf-8 and accents. While in Windows' Explorer, I assume filenames are encoded either in ANSI (cp1252) or utf-8, but what happens when calling them in a batch script? Seems like Windows does no conversion, hence the error.

Calling subprocess.run() is a work-around, although I'd still like to know how to create a .cmd file with filenames that contain accents:
p = subprocess.run(CMD, text=True, stdout=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW) output = f"run return code: {p.returncode}" print(output)
Reply
#4
check this Running BAT/CMD file with accented characters in it and see if it helps
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thanks for the pointer.

Saving the .cmd file as "ascii" may not work:
#UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 102: ordinal not in range(128) ascii = CMD.encode('ascii') with open(OUTPUT, 'wb') as f:	f.write(ascii)
I guess I'll just use subprocess when I need to call a CLI app with filenames that contain diacritics.
Reply
#6
did you check and try e.g. lumi's or metalcoder's answer. The later has link to other useful question.
Basically what it says is check console codepage, you may have to change it (it may be temporary in the bat file)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [Windows] Fails reading strings with accents Winfried 1 2,077 Apr-23-2023, 05:27 PM
Last Post: Larz60+
  Letters with Accents Not Inputting Phylus 0 2,158 Jun-12-2019, 04:05 PM
Last Post: Phylus
  Letters with Accents Not Displaying Phylus 5 9,544 Jun-12-2019, 05:43 AM
Last Post: Phylus

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.