Using shutil.make_archive with Windows results in OSError

  archive, path, python, windows

I am trying to create an archive using shutil.make_archive and it runs successfully on Linux but not on Windows.

Here is the code:

# 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_outputsoutputs_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)

And here is a stacktrace:

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:UsersEnzoMoliondevDataTeamlog-storytoolboxtools_for_file_handling.py", line 306, in move_old_outputs
    shutil.make_archive(rf"{archived_output_path}", 'zip', output_dir_path)
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0libshutil.py", line 1059, in make_archive
    filename = func(base_name, base_dir, **kwargs)
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0libshutil.py", line 940, in _make_zipfile
    with zipfile.ZipFile(zip_filename, "w",
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0libzipfile.py", line 1251, in __init__
    self.fp = io.open(file, filemode)
OSError: [Errno 22] Invalid argument: 'C:UsersEnzoMoliondevDataTeamlog-storyold_outputsoutputs_2021-08-26 17:44:34.189488.zip'

Why isn’t Windows version of make_archive happy with this path that looks like a valid windows path (doesn’t it?)?

Source: Windows Questions

LEAVE A COMMENT