将 shutil.make_archive 与 Windows 一起使用会导致 OSError
Using shutil.make_archive with Windows results in OSError
我正在尝试使用 shutil.make_archive
创建存档,它在 Linux 上成功运行,但在 Windows 上运行失败。
代码如下:
# archive_dir_path looks like this : old_outputs
# execution_datetime looks like this : 2021-08-26 17:44:34.189488
# archived_output_path looks like this : old_outputs\outputs_2021-08-26 17:44:34.189488
archived_output_path = os.path.join(archive_dir_path, f'outputs_{execution_datetime}')
shutil.make_archive(archived_output_path, 'zip', output_dir_path)
这是堆栈跟踪:
Traceback (most recent call last):
File "main_log_story.py", line 200, in <module>
tb.move_old_outputs(output_dir_path, filtered_logs_dir_path)
File "C:\Users\EnzoMolion\dev\DataTeam\log-story\toolbox\tools_for_file_handling.py", line 306, in move_old_outputs
shutil.make_archive(rf"{archived_output_path}", 'zip', output_dir_path)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\shutil.py", line 1059, in make_archive
filename = func(base_name, base_dir, **kwargs)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\shutil.py", line 940, in _make_zipfile
with zipfile.ZipFile(zip_filename, "w",
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\zipfile.py", line 1251, in __init__
self.fp = io.open(file, filemode)
OSError: [Errno 22] Invalid argument: 'C:\Users\EnzoMolion\dev\DataTeam\log-story\old_outputs\outputs_2021-08-26 17:44:34.189488.zip'
为什么 Windows 版本的 make_archive 不满意这条看起来像有效 windows 路径的路径(不是吗?)?
我发现问题是我创建的文件名包含一个分号 (:
),它 is not valid 在 Windows 文件名
中
我正在尝试使用 shutil.make_archive
创建存档,它在 Linux 上成功运行,但在 Windows 上运行失败。
代码如下:
# archive_dir_path looks like this : old_outputs
# execution_datetime looks like this : 2021-08-26 17:44:34.189488
# archived_output_path looks like this : old_outputs\outputs_2021-08-26 17:44:34.189488
archived_output_path = os.path.join(archive_dir_path, f'outputs_{execution_datetime}')
shutil.make_archive(archived_output_path, 'zip', output_dir_path)
这是堆栈跟踪:
Traceback (most recent call last):
File "main_log_story.py", line 200, in <module>
tb.move_old_outputs(output_dir_path, filtered_logs_dir_path)
File "C:\Users\EnzoMolion\dev\DataTeam\log-story\toolbox\tools_for_file_handling.py", line 306, in move_old_outputs
shutil.make_archive(rf"{archived_output_path}", 'zip', output_dir_path)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\shutil.py", line 1059, in make_archive
filename = func(base_name, base_dir, **kwargs)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\shutil.py", line 940, in _make_zipfile
with zipfile.ZipFile(zip_filename, "w",
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\zipfile.py", line 1251, in __init__
self.fp = io.open(file, filemode)
OSError: [Errno 22] Invalid argument: 'C:\Users\EnzoMolion\dev\DataTeam\log-story\old_outputs\outputs_2021-08-26 17:44:34.189488.zip'
为什么 Windows 版本的 make_archive 不满意这条看起来像有效 windows 路径的路径(不是吗?)?
我发现问题是我创建的文件名包含一个分号 (:
),它 is not valid 在 Windows 文件名