Ошибка OSError: error in opening %filename% for reading из-за кириллицы в пути
Мне требуется архивировать несколько путей на машине с Windows. Для архивации использую pyminizip, т.к. он позволяет установить пароль на архив. С помощью pathlib получаю список всех файлов по каждому из нужных путей, объединяю их в один список, далее преобразую в два списка строк - полный путь к файлу и путь до родительской папки - и затем, пытаюсь архивировать с помощью pyminizip.compress_multiple. На этапе архивирования возникала ошибка SystemError: <build-in function compress_multiple> returned NULL without setting an error.
Упрощая свой код, в итоге пришел к тому, что проблема кроется в файлах, в чьих полных именах есть кириллица. Сделал тестовую структуру файлов и пытаюсь на ней победить кириллицу.
D:
├─── test
│ ├─── latin_only
│ │ ├─── 1.txt
│ │ ├─── 2.txt
│ │ └─── ы.txt
│ │
│ ├─── с кириллицей
│ │ ├─── 11.txt
│ │ ├─── 22.txt
│ │ └─── ыы.txt
│ │
│ ├─── 111.txt
│ ├─── 222.txt
│ └─── ыыы.txt
...
from pathlib import Path
from pprint import pprint
import pyminizip
path_to_arc = Path('D:/test')
filepaths_to_arc = [p for p in path_to_arc.rglob('*') if p.is_file() and not p.stem.startswith('~$')]
list_file_path_and_prefix = {p.as_posix():p.parent.as_posix() for p in filepaths_to_arc}
# list_file_path_and_prefix вывел в консоль и разбил на два объекта: все файлы и файлы без кириллицы pprint(list_file_path_and_prefix)
all_files = {'D:/test/111.txt': 'D:/test',
'D:/test/222.txt': 'D:/test',
'D:/test/latin_only/1.txt': 'D:/test/latin_only',
'D:/test/latin_only/2.txt': 'D:/test/latin_only',
'D:/test/latin_only/ы.txt': 'D:/test/latin_only',
'D:/test/с кириллицей/11.txt': 'D:/test/с кириллицей',
'D:/test/с кириллицей/22.txt': 'D:/test/с кириллицей',
'D:/test/с кириллицей/ыы.txt': 'D:/test/с кириллицей',
'D:/test/ыыы.txt': 'D:/test'}
only_latin = {'D:/test/111.txt': 'D:/test',
'D:/test/222.txt': 'D:/test',
'D:/test/latin_only/1.txt': 'D:/test/latin_only',
'D:/test/latin_only/2.txt': 'D:/test/latin_only'}
try:
pyminizip.compress_multiple(list(only_latin.keys()), list(only_latin.values()), 'D:/test/arc_latin_only.zip', '1234', 2)
except Exception as e:
print(e)
try:
pyminizip.compress_multiple(list(all_files.keys()), list(all_files.values()), 'D:/test/arc_cyrillic.zip', '1234', 2)
except Exception as e:
print(e)
В итоге arc_latin_only.zip создается без проблем, а при создании второго возникает ошибка OSError: error in opening D:/test/latin_only/ы.txt
При этом сам файл без проблем читается через with open('D:/test/latin_only/ы.txt', 'r') as f, то есть windows понимает такое написание пути
Упрощал код до такого:
import pyminizip
only_latin = ['D:/test/111.txt', 'D:/test/222.txt', 'D:/test/latin_only/1.txt', 'D:/test/latin_only/2.txt']
all_files = ['D:/test/111.txt',
'D:/test/222.txt',
'D:/test/latin_only/1.txt',
'D:/test/latin_only/2.txt',
'D:/test/с кириллицей/11.txt',
'D:/test/с кириллицей/22.txt',
'D:/test/с кириллицей/ыы.txt',
'D:/test/ыыы.txt']
pyminizip.compress_multiple(only_latin, [], 'D:/test/arc_only_latin.zip', '1234', 2)
pyminizip.compress_multiple(all_files, [], 'D:/test/arc_cyrillic.zip', '1234', 2)
и та же самая ошибка OSError: error in opening D:/test/с кириллицей/11.txt
Замена / на \ ничего не дала - ошибка та же
Попробовал сделать через os.walk
import pyminizip
import os
path = 'D:/test'
os.chdir(path)
src_files = []
dest_paths = []
for folder, subfolder, files in os.walk('.'):
for file in files:
src_files.append(os.path.join(folder, file))
dest_paths.append(folder)
pyminizip.compress_multiple(src_files, dest_paths, 'D:/test/test.zip', '1234', 2)
В таком варианте ошибка OSError: error in opening .\ыыы.txt
Как можно решить проблему с кириллицей в путях в этом модуле?