初学Python常见的错误
2013-01-24 14:16
405 查看
1. TypeError: coercing to Unicode: need string or buffer, type found
代码为:
Traceback (most recent call last): File "unzip.py", line 20, in <module> tar = tarfile.open(file) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.py", line 1658, in open return func(name, "r", fileobj, **kwargs) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.py", line 1720, in gzopen fileobj = bltn_open(name, mode + "b") TypeError: coercing to Unicode: need string or buffer, type found
代码为:
#扫描当前目前下所有的gz文件 currDir = os.getcwd() files = os.listdir(currDir) dstDir = "201301_09" if not os.path.exists(dstDir): os.mkdir(dstDir) for fileName in files: if os.path.isfile(fileName) and tarfile.is_tarfile(fileName): print fileName try: tar = tarfile.open(file)#这里应该是fileName而不是file names = tar.getnames() for name in names: tar.extract(name,path=dstDir) tar.close() except tarfile.ReadError: print "not a tarFile" pass
相关文章推荐
- 常见Python运行时错误
- 新手常见Python运行时错误
- 第19节--python常见错误与异常处理
- python2-常见错误-不断更新
- Python3环境安装Scrapy爬虫框架过程及常见错误
- python常见的错误类型和继承关系
- 常见python错误(4,5,13,14,15,17点不错)
- 【Python】收集一些常见的错误,慢慢更新。
- python程序员常见的10个错误-python语言
- python常见运行错误
- <转>Python运行的17个时新手常见错误小结
- 17个新手常见Python运行时错误
- 【Python】17个新手常见Python运行时错误
- 17个新手常见Python运行时错误
- Python运行的17个时新手常见错误小结
- Python常见错误
- python 开发中的常见编程错误
- 17个新手常见Python运行时错误
- Python3环境安装Scrapy爬虫框架过程及常见错误
- Python程序员的10个常见错误