<html><head></head><body bgcolor="#FFFFFF"><div><br></div><div>On Jan 6, 2012, at 22:57, daedae11 <<a href="mailto:daedae11@126.com">daedae11@126.com</a>> wrote:<br><br></div><div></div><blockquote type="cite"><div>
<meta content="text/html; charset=us-ascii" http-equiv="Content-Type">
<style>
BLOCKQUOTE {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; MARGIN-LEFT: 2em
}
OL {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
UL {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
P {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
BODY {
        LINE-HEIGHT: 1.5; FONT-FAMILY: 微软雅黑; COLOR: #000000; FONT-SIZE: 10.5pt
}
</style>
<meta name="GENERATOR" content="MSHTML 9.00.8112.16440">
<div>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. </div>
<div> </div>
<div>my code is:</div>
<div> </div>
<div>
<div> </div>
<div>import zipfile;</div>
<div>import tarfile;</div>
<div>import os;</div>
<div>from os import path ;</div>
<div> </div>
<div>def showAllFiles(fileObj):</div>
<div> if fileObj.filename.endswith("zip"):</div>
<div> if isinstance(fileObj, zipfile.ZipFile):</div>
<div> print "j"*20;</div>
<div> for name in fileObj.namelist():</div>
<div> print name;</div>
<div> else:</div>
<div> for name in fileObj.getnames():</div>
<div> print name; </div>
<div> </div>
<div>def moveFile(srcObj, dstObj):</div>
<div> fileName = raw_input("input the name of the file to move: "); </div>
<div> srcObj.extract(fileName);</div>
<div> if isinstance(dstObj, zipfile.ZipFile):</div>
<div> dstObj.write(fileName);</div>
<div> else:</div>
<div> dstObj.addfile(tarfile.TarInfo(fileName));</div>
<div> os.remove(fileName); </div>
<div> </div>
<div>def main():</div>
<div> intro = """</div>
<div>enter a choice</div>
<div>(M)ove file from source file to destinatiom file</div>
<div>(S)how all the files in source file</div>
<div>(Q)uit</div>
<div>your choice is: """ </div>
<div> srcFile = raw_input("input the source file name: ");</div>
<div> dstFile = raw_input("input the destination file name: ");</div>
<div> while True:</div>
<div> with ( zipfile.ZipFile(srcFile, "r") if srcFile.endswith("zip") else tarfile.open(srcFile, "r"+":"+path.splitext(srcFile)[1][1:]) ) as srcObj, \</div>
<div> ( zipfile.ZipFile(dstFile, "r") if</div>
<div> dstFile.endswith("zip") else</div>
<div> tarfile.open(dstFile, "w"+":"+path.splitext(dstFile)[1][1:]) ) as dstObj: </div>
<div> choice = raw_input(intro)[0].lower();</div>
<div> if choice == "s":</div>
<div> showAllFiles(srcObj);</div>
<div> elif choice == "m":</div>
<div> moveFile(srcObj, dstObj);</div>
<div> elif choice == "q":</div>
<div> break;</div>
<div> else:</div>
<div> print "invalid command!"</div>
<div> </div>
<div>if __name__ == '__main__':</div>
<div> main();</div></div>
<div> </div>
<div>But there are some problems.</div>
<div>1. It could extract file successfully, but can't add files to .tar.gz
file.</div>
<div>2. I think it's a little tedious, but I don't know how to improve it.</div>
<div> </div>
<div>Please give me some help , thank you!</div>
<div> </div>
<hr style="WIDTH: 210px; HEIGHT: 1px" align="left" color="#b5c4df" size="1">
<div><span>daedae11</span></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a></span><br><span>To unsubscribe or change subscription options:</span><br><span><a href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a></span><br></div></blockquote><br><div>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?</div><div>Looking forward to your response. </div><div>Alexander</div></body></html>