472,958 Members | 2,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Keep windows from zipping up a file.

30
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
1 1295
bvdet
2,851 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

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

Similar topics

5
by: comp.lang.php | last post by:
Has anyone had any luck with file zipping utilities for PHP 4-5 and Windows XP with Apache 2.0.53? Following is my code snippet: PHP Code: if ($this->isSuccessful) { // RUN ZIP...
0
by: Antonio | last post by:
Hi, I am building a windows service that does the following 1) a System.Timers.Timer elapseed event handler will run code at every interval, maybe every 2 to 5 minutes or so (i have not decided...
0
by: Benjamin Bittner | last post by:
hallo ng, first of all, for the zipping progress i use the ziplib from http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx now to my problem. im trying to build a little application...
6
by: Shimon Sim | last post by:
Hi 1) Is it possible to force browser to open image in default windows application? That is what specifies in Folder Options for jpg extension. 2) Also if the 1) is possible is it possible to...
9
by: ReidarT | last post by:
How can I zip (compress) a file in vs2005 with windows own zip-program? reidarT
4
by: sri2097 | last post by:
Hi all,This is in reply to the 'Compressing folders in Windows using Python' query I raised y'day. I figured out that windows does not allow command line zipping so I started looking for...
2
by: johnny | last post by:
I have the following code. What I need is to zip the file (c:\web\upload\someFile) to d:\web\someFile.zip. $cmd = 'c:\zip\zip.exe '. $movedFile. ' '. $finalFile; exec( $cmd, $cmd_output ); ...
1
by: durumdara | last post by:
Hi! As I experienced in the year 2006, the Python's zip module is not unicode-safe. With the hungarian filenames I got wrong result. I need to convert iso-8859-2 to cp852 chset to get good...
5
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
This code works fine in Windows Application. In Windows Application, I am able to zip the image files properly and it totally contains 900MB My problem is the same code which I used in my Windows...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.