467,912 Members | 1,693 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,912 developers. It's quick & easy.

Keep windows from zipping up a file.

I wrote this script...

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. # Filename: backup_zip.py
  3.  
  4. import os, zipfile, time, datetime, glob
  5. from os.path import splitext, relpath, split
  6.  
  7. r = 1
  8. cwd = os.getcwd()
  9. today = datetime.date.today()
  10.  
  11. def backup():
  12.  
  13.    for dirpath,dirs,files in os.walk(cwd):
  14.  
  15.       for file in files:
  16.  
  17.          z_path = os.path.relpath(dirpath)
  18.          filename = os.path.split(cwd)[1]
  19.          arcfile_name = r"%s_%s.zip" % (filename,today)
  20.          fileList = [z_path] 
  21.  
  22.          output = zipfile.ZipFile(arcfile_name, 'w', zipfile.ZIP_DEFLATED)
  23.  
  24.          print "\n "
  25.          for i in fileList:
  26.             if i == 'backup_zip.py':
  27.                print '- backup_zip.py'
  28.             else:
  29.                print "archiving file '%s'" % (i)
  30.                output.write(i)
  31.          output.close()
  32.          print "\n "
  33.  
  34.  
  35. if __name__=='__main__':
  36.  
  37.    while r == 1:
  38.  
  39.       print "This script when run backs up files in a directory by comressing them to zips. "
  40.       begin = raw_input("Do you wish to back up files in this directory [type: yes or no] ")   
  41.  
  42.       if begin == "yes":
  43.          r = 1
  44.          backup()
  45.          time.sleep(8)
  46.          r = 0
  47.          break
  48.       elif begin == "no":
  49.          r = 0
  50.       else:
  51.          print "Enter yes or no"
  52.  
  53.  
  54.  
  55.  
the script is in a file called backup_zip.py, and when I run the script its purpose is to zip up files in the directory that the script is in. However, currently it also zips up backup_zip.py (the script I wrote) and I don't want that. I tried preventing that above with the if i == 'backup.. but it didnt' work. How would i do this?
Oct 22 '08 #1
  • viewed: 1160
Share:
1 Reply
bvdet
Expert Mod 2GB
I took out some things that I thought unnecessary, and moved assignment of output out of the for loop. This will skip the script.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. # Filename: backup_zip.py
  3.  
  4. import os, zipfile, time, datetime, glob
  5. from os.path import splitext, split
  6.  
  7. cwd = os.getcwd()
  8. today = datetime.date.today()
  9.  
  10. def backup():
  11.     filename = os.path.split(cwd)[1]
  12.     arcfile_name = r"%s_%s.zip" % (filename,today)
  13.     output = zipfile.ZipFile(arcfile_name, 'w', zipfile.ZIP_DEFLATED)
  14.  
  15.     for dirpath,dirs,files in os.walk(cwd):
  16.         for f in files:
  17.             fn = os.path.join(dirpath, f)
  18.             if f == 'backup_zip.py':
  19.                 print '- backup_zip.py'
  20.             else:
  21.                 print "archiving file '%s'" % (fn)
  22.                 output.write(fn)
  23.     output.close()
  24.     print "\n "
  25.  
  26.  
  27. if __name__=='__main__':
  28.  
  29.     print "This script when run backs up files in a directory by comressing them to zips. "
  30.     begin = raw_input("Do you wish to back up files in this directory [type: yes or no] ")   
  31.  
  32.     if begin == "yes":
  33.         backup()
You should realize that this archives files in subdirectories under your current working directory.
Oct 23 '08 #2

Post your reply

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

Similar topics

reply views Thread by Antonio | last post: by
reply views Thread by Benjamin Bittner | last post: by
6 posts views Thread by Shimon Sim | last post: by
4 posts views Thread by sri2097 | last post: by
2 posts views Thread by johnny | last post: by
1 post views Thread by durumdara | last post: by
5 posts views Thread by =?Utf-8?B?dmlzaHJ1dGg=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.