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...
It is giving me this error..
I got this part from another script I saw..
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.
Code:
#!/usr/bin/python # Filename: backup_zip.py import os import zipfile import glob r = 1 def backup(): cwd = os.getcwd() # for file in os.walk(cwd): for dirpath, dirs, files in os.walk(cwd): z_path = os.path.join(dirpath,file) # docs = open(file, 'r') docs = open(z_path, 'r') output = zipfile.ZipFile(docs, 'w') output.write(docs) output.close() # Ignore old code I was messing with \\\\\\\ # ZipFile(cwd['w'[ZIP_STORED[allowZip64]]]): # z_path = os.path.join(dirpath,file) # start = cwd.rfind( # output.write(z_path,z_path[start:]) # # if file == '*.zip': # # else: # output.write(z_path) # output.close() # # output_append.write(z_path) 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() elif begin == "no": r = 0 else: print "Enter yes or no"
Code:
z_path = os.path.join(dirpath,file) File "C:\Python25\lib\ntpath.py", line 67, in join elif isabs(b): File "C:\Python25\lib\ntpath.py", line 53, in isabs s = splitdrive(s)[1] File "C:\Python25\lib\ntpath.py", line 119, in splitdrive if p[1:2] == ':': TypeError: 'type' object is unsubscriptable
Code:
for dirpath, dirs, files in os.walk(cwd): z_path = os.path.join(dirpath,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.
Comment