I’m trying to write a python script that will create a ZIP inside a “backups” directory that includes all the contents of the directory.
My file structure:
C:/
Users/
myuser/
backups/
blah.gif
config.xml
etc.
So I want the script to create a C:/Users/myuser/backups/mybackup.zip that contains all the contents of the backups/ directory (blah.gif, config.xml, etc.).
Here is the code I’m using:
backupDir = "C:/Users/myuser/backups/"
backupFile = "mybackup.zip"
file = zipfile.ZipFile(backupDir + backupFile, "w")
for name in glob.glob(backupDir):
print "Found " + name
file.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)
file.close()
When I run this it produces a C:/Users/myuser/backups/mybackup.zip file but when I unzip it, it’s empty. Where am I going astray? Thanks in advance!
glob expect a pattern match. I prefer to use os.listdir() because it is easier to use.