[Tutor] Zip, tar, and file handling

Alexander Etter rhettnaxel at gmail.com
Sun Jan 8 17:54:24 CET 2012


On Jan 6, 2012, at 22:57, daedae11 <daedae11 at 126.com> wrote:

> I was asked to write a program to move files between ZIP(.zip) and TAR/GZIP(.tgz/.tar.gz) or TAR/BZIP2(.tbz/.tar.bz2) archive.
>  
> my code is:
>  
>  
> import zipfile;
> import tarfile;
> import os;
> from os import path ;
>  
> def showAllFiles(fileObj):
>     if fileObj.filename.endswith("zip"):
>         if isinstance(fileObj, zipfile.ZipFile):
>             print "j"*20;
>         for name in fileObj.namelist():
>             print name;
>     else:
>         for name in fileObj.getnames():
>             print name; 
>  
> def moveFile(srcObj, dstObj):
>     fileName = raw_input("input the name of the file to move: ");    
>     srcObj.extract(fileName);
>     if isinstance(dstObj, zipfile.ZipFile):
>         dstObj.write(fileName);
>     else:
>         dstObj.addfile(tarfile.TarInfo(fileName));
>     os.remove(fileName);    
>     
> def main():
>     intro = """
> enter a choice
> (M)ove file from source file to destinatiom file
> (S)how all the files in source file
> (Q)uit
> your choice is: """    
>     srcFile = raw_input("input the source file name: ");
>     dstFile = raw_input("input the destination file name: ");
>     while True:
>         with ( zipfile.ZipFile(srcFile, "r") if srcFile.endswith("zip") else tarfile.open(srcFile, "r"+":"+path.splitext(srcFile)[1][1:]) ) as srcObj, \
>         ( zipfile.ZipFile(dstFile, "r") if
>            dstFile.endswith("zip") else
>             tarfile.open(dstFile, "w"+":"+path.splitext(dstFile)[1][1:]) ) as dstObj:        
>                 choice = raw_input(intro)[0].lower();
>                 if choice == "s":
>                     showAllFiles(srcObj);
>                 elif choice == "m":
>                     moveFile(srcObj, dstObj);
>                 elif choice == "q":
>                     break;
>                 else:
>                     print "invalid command!"
>  
> if __name__ == '__main__':
>     main();
>  
> But there are some problems.
> 1. It could extract file successfully, but can't add files to .tar.gz file.
> 2. I think it's a little tedious, but I don't know how to improve it.
>  
> Please  give me some help , thank you!
>  
> daedae11
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Hi there. I would start by handling file extensions other than ZIP in your first two functions. Why not handle if the file is a tgz or tbz within the functions. Also I don't see the purpose of the first function, "showallfiles" it prints out twenty "j"s?
Looking forward to your response. 
Alexander
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120108/2eb44e6a/attachment.html>


More information about the Tutor mailing list