0

According to this post I'm calling the zip command using os.system() in Python.

At the command line it works:

zip -r /Backups/backups/20152011-120209{.zip,}

When I call this from a Python script (PATH is "/Backups/backups/20152011-120209")

os.system("zip -r " + PATH + "{.zip,}")

It throws:

zip error: Nothing to do! (/Backups/backups/20152011-122909{.zip,})

What am I doing wrong?

I want to zip a directory (including its content) to a zip file with the same name at the same place (a script dumps my MySQL databases to *.sql files and I want to zip the files after that).

  • 2
    Not at home right now, but using os.system at all is a really bad idea. Outdated and deprecated. Use subprocess.call() or subprocess.Popen() intead. – Jacob Vlijm Nov 20 '15 at 12:45
2

Before getting to the problem, I'll quote Jacob Vlijm's comment under this answer (thanks for the comment and for the link):

[...] using os.system at all is a really bad idea. Outdated and deprecated. Use subprocess.call() or subprocess.Popen() intead.

Here's the first (or one of the first) deprecation proposal, dated back to 2010.

So you should really use subprocess.Popen() instead of os.system().


When you run os.system() the command is executed in Dash (/bin/sh), while when you run the command in a terminal the command is executed in Bash (/bin/bash);

Dash doesn't support brace expansion and interprets {.zip,} literally;

Run the command in Bash: change

os.system("zip -r " + PATH + "{.zip}")

to

os.system("bash -c 'zip -r " + PATH + "{.zip,}'")

Or anyway as Darael suggests FWIW passing /Backups/backups/20152011-120209{.zip,} to expand /Backups/backups/20152011-120209 to /Backups/backups/20152011-120209.zip and /Backups/backups/20152011-120209 you might as well just pass the paths directly avoiding to spawn another shell:

os.system("zip -r " + PATH + ".zip " + PATH)
  • Thank you kos for your comment. Unfortunately it was mistyped in my question - in my script there was an "," at the end. I corrected my original question. So, you aure right, but it does not solve my problem :( – Benninger Nov 20 '15 at 11:26
  • @Benninger Maybe you mistyped again, but the comma goes before the closing brace, here's what you just posted: os.system("zip -r " + PATH + "{.zip},"). – kos Nov 20 '15 at 11:28
  • Yes I mistyped it again, sorry for that. In my script os.system("zip -r " + PATH + "{.zip,}") is used and it shows still zip error: Nothing to do! (/Backups/backups/20152011-122909{.zip,}) – Benninger Nov 20 '15 at 11:30
  • 1
    The curly brackets are a bash feature. I'm guessing that python's os.system() either bypasses shells entirely or uses a shell (probably /bin/sh, which in Ubuntu is dash, which I think fits) that doesn't support the syntax. Whichever is the case, the solution is to expand in your python script to os.system("zip -r "+PATH+".zip "+PATH") – Darael Nov 20 '15 at 11:46
  • @Darael That's absolutely correct, I just figured that out and updated the answer. – kos Nov 20 '15 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.