自信,努力,坚持
python 文件操作
上一篇 /
下一篇 2011-08-08 14:36:35
/ 个人分类:python
1、解压zip包
用到的库zipfile
import zipfile
if zipfile.is_zipfile(zfile) != True:
assert zipfile.is_zipfile(zfile) == True, "the zfile
is not zipfile"
f=zipfile.ZipFile(zfile) //zfile是指要解压的zip包
nlist=f.namelist()
for n in nlist:
print "a "+n
open(path + n,'a').write(f.read(n))
f.close()
以上方法解压后的文件无法打开,找不到原因,有知道的可以指点一二,谢谢。
改用以下方法:
f=zipfile.ZipFile(zfile, mode='r')
f.extractall(os.path.join(path))
f.close()
2、创建文件夹
if os.path.exists(path) != True:
os.mkdir("result")
3、清空文件夹内所有文件及目录
for root, files in os.walk(path):
for name in files:
os.remove(os.path.join(root, name))
os.walk可遍历path目录,如果有子目录同样可展开
os.path.join(root, name)) 所在目录+文件名就是该文件的完整路径
4、比较文件夹
filecmp.cmp(f1, f2[, shallow]):
http://edu.codepub.com/2010/0624/23763.php
收藏
举报
TAG: