Problem in python code! please i need help


#1

‘zip’ is not recognized as an internal or external command,
operable program or batch file.
backup failed

n.py:
`import os
import time

source=[r’C:\Users\AiMeN\Desktop\a’]
where=’/This PC/Documents’
ntarget=where + time.strftime(’%Y%m%d%H%M%S’) + ‘.zip’
zip_command=“zip -qr’%s’%s”%(ntarget,’’.join(source))

if os.system(zip_command) == 0:
print(“success backupto”,ntarget)
else :
print(“backup failed”)
`


#2

The error you’re getting:

'zip' is not recognized as an internal or external command,
operable program or batch file.

is being returned by your underlying OS, not Python.

So, your line:

zip_command=“zip -qr’%s’%s”%(ntarget,’’.join(source))

must be wrong, somehow. :slight_smile: (Presumably because zip is not a native command in Windows.)


#3

I am learning Python and trying to write a program that makes a backup of all my important files and put them in a compressed file…
what’s the solution???


#5

I have assumed that your operating system is Windows, because I don’t know of a *nix operating system that would return the error you’re seeing.

The problem, as I said previously, is that you’re asking Windows to run the following command in its native shell:

zip -qr '/This PC/Documents{timestampe}.zip' C:\\Users\\AiMeN\Desktop\a

Your error is basically Windows telling you that it doesn’t know a command called zip. If you open the command line and type in zip you’ll get exactly the same error as your Python program is giving you.

If you have installed a third-party compression tool, like WinZIP or 7-zip (Other utilities are available - Ed), then your zip_command should point to the command line version of that. For example, on x64 Windows, the path to the 7-zip command line tool is C:\Program Files (x86)\7-Zip\7z.exe.

Do you see now why your zip_command variable is wrong? :slight_smile:

Edit: I had to check; zip is a command available on my Linux distro, but it’s certainly not in Windows. Also, if I bork my command on Linux, I get command not found… so now I’m 99.9% certain you’re on Windows.


#6

If you want to compress a file or file use this

Example of how to create a compressed GZIP file:

import gzip
content = “Lots of content here”
with gzip.open(‘file.txt.gz’, ‘wb’) as f:
f.write(content)

Example of how to GZIP compress an existing file:

import gzip
import shutil
with open(‘file.txt’, ‘rb’) as f_in, gzip.open(‘file.txt.gz’, ‘wb’) as f_out:
shutil.copyfileobj(f_in, f_out)

(https://docs.python.org/2/library/gzip.html)

Let me know if this works for you.