By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,997 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,997 IT Pros & Developers. It's quick & easy.

Backup with zips

P: 30
I am attempting to write a script to zip all directories in current directory into zip files. e.g. [If a directory has 2 folders in it and my script is run in that directory I want it to create two zips (one for each folder) and keep the tree organization in the zip]. Currently I have this...

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. # Filename: backup_zip.py
  3.  
  4. import os
  5. import zipfile
  6. import glob
  7.  
  8. r = 1
  9.  
  10. def backup():
  11.  
  12.    cwd = os.getcwd()
  13.  
  14.    # for file in os.walk(cwd):
  15.    for dirpath, dirs, files in os.walk(cwd):
  16.       z_path = os.path.join(dirpath,file) 
  17.    #   docs = open(file, 'r')
  18.       docs = open(z_path, 'r')
  19.       output = zipfile.ZipFile(docs, 'w')
  20.       output.write(docs)
  21.  
  22.    output.close()
  23.  
  24.   # Ignore old code I was messing with \\\\\\\
  25.   # ZipFile(cwd['w'[ZIP_STORED[allowZip64]]]):
  26.   #   z_path = os.path.join(dirpath,file)
  27.   #   start = cwd.rfind(
  28.   #   output.write(z_path,z_path[start:])
  29.   #
  30.   # if file == '*.zip':
  31.   #     
  32.   # else:
  33.   #   output.write(z_path)
  34.   # output.close()
  35.   #
  36.   # output_append.write(z_path)
  37.  
  38. while r == 1:
  39.  
  40.    print "This script when run backs up files in a directory by comressing them to zips. "
  41.    begin = raw_input("Do you wish to back up files in this directory [type: yes or no] ")   
  42.  
  43.    if begin == "yes":
  44.       r = 1
  45.  
  46.       backup()
  47.    elif begin == "no":
  48.       r = 0
  49.    else:
  50.       print "Enter yes or no"
  51.  
  52.  
It is giving me this error..

Expand|Select|Wrap|Line Numbers
  1.     z_path = os.path.join(dirpath,file)
  2.   File "C:\Python25\lib\ntpath.py", line 67, in join
  3.     elif isabs(b):
  4.   File "C:\Python25\lib\ntpath.py", line 53, in isabs
  5.     s = splitdrive(s)[1]
  6.   File "C:\Python25\lib\ntpath.py", line 119, in splitdrive
  7.     if p[1:2] == ':':
  8. TypeError: 'type' object is unsubscriptable
I got this part from another script I saw..

Expand|Select|Wrap|Line Numbers
  1. for dirpath, dirs, files in os.walk(cwd):
  2.       z_path = os.path.join(dirpath,file) 
I don't really know what it does. I know os.walk(cwd) walks through and creates a tuple or list of files in the directories, but I don't understand why dirpath dirs files are in there. (wouldn't it just need dir and file)

Also I don't understand why I need to join dirpath and file.

Please explain how to make this work or why mine doesn't work. Please keep in mind I am new to programming so I won't understand answers with a ton of jargon in them.
Oct 11 '08 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You are trying to join the path and the Python built-in file.
Expand|Select|Wrap|Line Numbers
  1. >>> file
  2. <type 'file'>
  3. >>> type(file)
  4. <type 'type'>
  5. >>> help(file)
  6. Help on class file in module __builtin__:
  7.  
  8. class file(object)
  9.  |  file(name[, mode[, buffering]]) -> file object
  10.  |  
  11.  |  Open a file.  The mode can be 'r', 'w' or 'a' for reading (default),
Oct 11 '08 #2

bvdet
Expert Mod 2.5K+
P: 2,851
How would ZipFile() find the files if you don't give it the full path? Here's an example:
Expand|Select|Wrap|Line Numbers
  1. import zipfile, os
  2.  
  3. def makeArchive(fileList, archive):
  4.     """
  5.     'fileList' is a list of file names - full path each name
  6.     'archive' is the file name for the archive with a full path
  7.     """
  8.     try:
  9.         # ZipFile will accept a file name or file object
  10.         a = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
  11.         for f in fileList:
  12.             print "archiving file %s" % (f)
  13.             a.write(f) # (f, os.path.basename(f))
  14.         a.close()
  15.         return True
  16.     except: return False
Oct 11 '08 #3

P: 30
When I run your code it doesn't give any errors. However, it doesn't work. When I run this as my code...

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. # Filename: backup_zip.py
  3.  
  4. import os, zipfile
  5.  
  6. r = 1
  7.  
  8. while r == 1:
  9.  
  10.    print "This script when run backs up files in a directory by comressing them to zips. "
  11.    begin = raw_input("Do you wish to back up files in this directory [type: yes or no] ")   
  12.  
  13.    if begin == "yes":
  14.       r = 1
  15.       backup()  
  16.    elif begin == "no":
  17.       r = 0
  18.    else:
  19.       print "Enter yes or no"
  20.  
  21. def backup(fileList, archive):
  22.  
  23.    cwd = os.getcwd()
  24. #   archive = os.pathname
  25. #   fileList = dirpath, dirs, files in os.walk(cwd)
  26.    f = os.path.basename
  27.  
  28.    output = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
  29.  
  30.    for f in fileList:
  31.       print "archiving file '%s'" % (f)
  32.       output.write(f)
  33.    output.close()
  34.  
  35. if __name__ == "__main__":
  36.    backup()
  37.  
  38.  
  39.  
  40.  
it gives me this error...

Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "C:\Documents and Settings\Gabe\Desktop\test\backup_zip.py", line 15, in <module>
  3.     backup()
  4. TypeError: backup() takes exactly 2 arguments (0 given)
I don't understand because you didn't define fileList or archive as a variable outside of the function (which is an argument right) Neither do i, but your code runs..?

Basically at this point my code is similar to yours but doens't run. I don't know what the error it gives means, and your code runs but doesn't create a zip file..
Oct 11 '08 #4

bvdet
Expert Mod 2.5K+
P: 2,851
You have to pass arguments to backup(). Regarding my code - when you run it, the function object is created, but is not called. Here's a an example of calling the function and passing arguments:
Expand|Select|Wrap|Line Numbers
  1. if __name__== '__main__':
  2.  
  3.     fileList = [r'D:\Miscellaneous\Toll Plaza Iso1.bmp',
  4.                 r'D:\Miscellaneous\Io_Volcano_Plume.jpg',
  5.                 r'D:\Miscellaneous\Dart.jpg'
  6.                 ]
  7.  
  8.     arcfile_name = r'D:\Miscellaneous\zipped_files.zip'
  9.  
  10.     if makeArchive(fileList, arcfile_name):
  11.         print arcfile_name, "was created.\n"
  12.         # check the new archive file
  13.         f = zipfile.ZipFile(arcfile_name, 'r')
  14.         for info in f.infolist():
  15.             print info.filename, info.date_time, info.file_size, info.compress_size
  16.         f.close()
  17.     else:
  18.         print "There was an error"
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> archiving file D:\Miscellaneous\Toll Plaza Iso1.bmp
  2. archiving file D:\Miscellaneous\Io_Volcano_Plume.jpg
  3. archiving file D:\Miscellaneous\Dart.jpg
  4. D:\Miscellaneous\zipped_files.zip was created.
  5.  
  6. D:/Miscellaneous/Toll Plaza Iso1.bmp (2001, 2, 1, 5, 8, 2) 5224250 130217
  7. D:/Miscellaneous/Io_Volcano_Plume.jpg (2007, 5, 1, 20, 43, 46) 102987 80937
  8. D:/Miscellaneous/Dart.jpg (2006, 7, 6, 12, 18, 20) 3506067 3060813
Oct 11 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.