Windows系统下Python压缩和解压文件、文件夹操作

jopen 10年前

# coding:UTF-8    import zipfile,os        #把整个文件夹内的文件打包    path = 'G:\\BaiduMusic\\Images\\'    zipName = path + 'BaiduMusic_Image.zip'        f = zipfile.ZipFile( zipName, 'w', zipfile.ZIP_DEFLATED )    for dirpath, dirnames, filenames in os.walk( path ):        for filename in filenames:            print filename            f.write(os.path.join(dirpath,filename))    f.close()        #将打包的文件解压    f = zipfile.ZipFile(zipName, 'r')    for file in f.namelist():        f.extract(file, path)

1、本实例在windows 7上面测试通过,linux下面路径需要改一下。

2、zipfile的介绍详见:http://docs.python.org/2/library/zipfile.html


来自:http://blog.csdn.net/ekeuy/article/details/18303555