【问题标题】:TypeError: Type str doesn't support the buffer API for stringTypeError:类型 str 不支持字符串的缓冲区 API
【发布时间】: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


【解决方案1】:

Python 2.6、2.7、3.x 的代码

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):
        fullname = os.path.join(root, filename)
        with open(fullname, 'rb') as infile:
            data = infile.read()
        output = None

        if b'SETQ' in data and b'findfile' in data and b'getvar' in data:
            output = "ACAD_LSP"
        elif b'base.dcl' in data:
            output = 'ACAD_LSP'
        elif b'vl-file-copy' in data:
            output = 'ACAD_LSP'
        elif b'FAS4-FILE' in data:
            output = 'ACAD_FAS'
        elif b'Autodesk' in data:
            output = 'ACAD_LSP'
        elif b'acad.lsp' in data:
            output = 'ACAD_LSP'

        if output:
            f.write("{0}, {1}\n".format(fullname, output).encode(utf-8'))
f.close()

更短的清晰代码更好。 DRY 原则:不要重复自己。

【讨论】:

    猜你喜欢
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    相关资源
    最近更新 更多