Keep windows from zipping up a file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bouzy
    New Member
    • Jun 2007
    • 30

    Keep windows from zipping up a file.

    I wrote this script...

    Code:
    #!/usr/bin/python
    # Filename: backup_zip.py
    
    import os, zipfile, time, datetime, glob
    from os.path import splitext, relpath, split
    
    r = 1
    cwd = os.getcwd()
    today = datetime.date.today()
                 
    def backup():
    
       for dirpath,dirs,files in os.walk(cwd):
    
          for file in files:
       
             z_path = os.path.relpath(dirpath)
             filename = os.path.split(cwd)[1]
             arcfile_name = r"%s_%s.zip" % (filename,today)
             fileList = [z_path] 
     
             output = zipfile.ZipFile(arcfile_name, 'w', zipfile.ZIP_DEFLATED)
    
             print "\n "
             for i in fileList:
                if i == 'backup_zip.py':
                   print '- backup_zip.py'
                else:
                   print "archiving file '%s'" % (i)
                   output.write(i)
             output.close()
             print "\n "
    
    
    if __name__=='__main__':
          
       while r == 1:
    
          print "This script when run backs up files in a directory by comressing them to zips. "
          begin = raw_input("Do you wish to back up files in this directory [type: yes or no] ")   
    
          if begin == "yes":
             r = 1
             backup()
             time.sleep(8)
             r = 0
             break
          elif begin == "no":
             r = 0
          else:
             print "Enter yes or no"
    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?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I took out some things that I thought unnecessary, and moved assignment of output out of the for loop. This will skip the script.[code=Python]#!/usr/bin/python
    # Filename: backup_zip.py

    import os, zipfile, time, datetime, glob
    from os.path import splitext, split

    cwd = os.getcwd()
    today = datetime.date.t oday()

    def backup():
    filename = os.path.split(c wd)[1]
    arcfile_name = r"%s_%s.zip" % (filename,today )
    output = zipfile.ZipFile (arcfile_name, 'w', zipfile.ZIP_DEF LATED)

    for dirpath,dirs,fi les in os.walk(cwd):
    for f in files:
    fn = os.path.join(di rpath, f)
    if f == 'backup_zip.py' :
    print '- backup_zip.py'
    else:
    print "archiving file '%s'" % (fn)
    output.write(fn )
    output.close()
    print "\n "


    if __name__=='__ma in__':

    print "This script when run backs up files in a directory by comressing them to zips. "
    begin = raw_input("Do you wish to back up files in this directory [type: yes or no] ")

    if begin == "yes":
    backup()[/code]You should realize that this archives files in subdirectories under your current working directory.

    Comment

    Working...