0

I am trying to zip a folder using python. The following command works from command prompt:

7z a C:\\Temp\\ C:\\Temp\\

But the following only show an error code of 1:

os.system("7z a C:\\Temp\\ C:\\Temp\\")

subprocess.call(["7z a C:\\Temp\\ C:\\Temp\\"], shell=True)

os.system("C:\Program Files\7-Zip\7z.exe a C:\\Temp\\ C:\\Temp\\")

Calling exe's with full path to them works for me well with the above approaches. Could anyone suggest a solution?

martineau
  • 119,623
  • 25
  • 170
  • 301
illan
  • 163
  • 1
  • 13
  • Have you tried using `shutil`? [How to zip folder in Python](https://stackoverflow.com/questions/1855095/how-to-create-a-zip-archive-of-a-directory) – Jack Wotherspoon Jan 27 '22 at 21:25
  • @JacK: `shutil` doesn't do 7zip compression. – martineau Jan 27 '22 at 22:03
  • When a file path has spaces in it you have to enclose the whole thing in double-quote characters. Try `os.system('"C:\\Program Files\\7-Zip\\7z.exe" a C:\\Temp\\ C:\\Temp\\')` – martineau Jan 27 '22 at 22:16
  • If my preceding suggestion works but `7z` by itself doesn't, it means the `C:\Program Files\7-Zip` directory hasn't been added to the system's `PATH` environment variable. The registry can also be changed to make it work, see [Are there any alternatives to adding to the `PATH` environment variable in Windows?](https://superuser.com/questions/189370/are-there-any-alternatives-to-adding-to-the-path-environment-variable-in-windows/599182) – martineau Jan 27 '22 at 22:24
  • @martineau Thanks, adding quotes worked. Any idea why I can use 7z directly in command prompt but have to specify full path in python os.system? The Windows System environmental variable does contain C:\Program Files\7-Zip. – illan Jan 28 '22 at 18:52
  • @JackWotherspoon Thanks. Using shutil is a simple workaround and more portable across systems/users because it doesn't require 7-Zip to be installed. I think I may use this approach. – illan Jan 28 '22 at 18:53
  • No, I don't know why something that worked at the command prompt without a full path would only work with one with `os.system()`. I seldom use the latter (preferring instead the more powerful `subprocess` module) as well as strive to write code that does *not* depend on external things being a certain way (such as the `PATH` environment variable or the current working directory) — in other words I strongly prefer using absolute paths. – martineau Jan 28 '22 at 19:25

1 Answers1

1

Try:

os.system("C:\\"Program Files\"\7-Zip\7z.exe a C:\\Temp\\ C:\\Temp\\")

add => " in (program file section)

martineau
  • 119,623
  • 25
  • 170
  • 301
i0xb4d
  • 26
  • 1
  • 1
    While this code *may* answer the question, it would be better to include some context, explaining _how_ it works and _when_ to use it. Code-only answers are not useful in the long run. – martineau Jan 27 '22 at 22:00
  • Thanks, that fixed the issue! I am confused why using 7z without prepanding path to it works in command prompt, but not from python os/subprocess commands. Any idea? – illan Jan 28 '22 at 18:50