【发布时间】:2014-10-14 16:49:39
【问题描述】:
rootPath = arg
pattern = '*.*'
f = open('facad.csv', 'w')
fname = 'facad.csv'
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
if 'SETQ' and 'findfile' and 'getvar' in open(os.path.join(root, filename), 'rb').read():
data = os.path.join(root, filename)+", ACAD_LSP\n"
f.write(data)
elif 'base.dcl' in open(os.path.join(root, filename)).read():
data = os.path.join(root, filename)+", ACAD_LSP\n"
f.write(data)
elif 'vl-file-copy' in open(os.path.join(root, filename)).read():
data= os.path.join(root, filename)+", ACAD_LSP\n"
f.write(data)
elif 'FAS4-FILE' in open(os.path.join(root, filename)).read():
data = os.path.join(root, filename)+", ACAD_FAS\n"
f.write(data)
elif 'Autodesk' in open(os.path.join(root, filename)).read():
data = os.path.join(root, filename)+", ACAD_LSP\n"
f.write(data)
elif 'acad.lsp' in open(os.path.join(root, filename)).read():
data = os.path.join(root, filename)+", ACAD_LSP\n"
f.write(data)
f.close()
我收到此错误:
C:\Python33>python D:\python\ftacad.py G:\ginipig\acad_and_lisp\ACAD_samples
Traceback (most recent call last):
File "D:\python\ftacad.py", line 62, in <module>
main()
File "D:\python\ftacad.py", line 40, in main
if 'SETQ' and 'findfile' and 'getvar' in open(os.path.join(root, filename),' rb').read():
TypeError: Type str doesn't support the buffer API
我有各种类型的文件要搜索。 代码不仅在 3.x 中运行。 在这个版本的 Python 下运行良好。
【问题讨论】:
-
错误在哪一行?
-
您需要向我们提供您异常的完整回溯;这意味着您需要传入字节,而不是 unicode 字符串。
-
另外,您的第一个
if条件有问题;即使在 Python 2 中,您的代码也没有按设计工作。请参阅How do I test one variable against multiple values? -
您还阅读每个文件六次,使您的代码非常慢。只需读取一次文件。
-
有人可以在我检查 3 个条件的地方给我写第一个 if 语句
标签: python