S
S
Sergey Karbivnichy2021-09-13 23:11:35
Python
Sergey Karbivnichy, 2021-09-13 23:11:35

How to create a file with Cyrillic in the file name in Windows?

There is an f.txt (utf-8) file with the following content:

один
два
три

and a python script:
with open('f.txt') as file:
  lines = file.read().splitlines()

for line in lines:
  print(line)
  with open(line,'w') as file:
    file.write('')

In Linux, of course, everything works without problems, but in Windows, instead of Cyrillic, these characters are:
613fb03fca662764228353.png
How can I solve the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Kuznetsov, 2021-09-14
@hottabxp

Maybe that will work?

with open('f.txt') as file:
    lines = file.read().splitlines()

for line in lines:
    print(line)
    with open(line.encode('utf-8'), 'w') as file:
        file.write('')

Here we explicitly indicate that the file name is in UTF-8 encoding, and not in OEM single-byte encoding.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question