0
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()

I am getting this error:

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

I have various types of files to search. Code not running only in 3.x. Runs fine below this version of Python.

5
  • On which line is the error? Commented Oct 14, 2014 at 7:31
  • You need to give us the full traceback of your exception; it means you need to pass in bytes, not a unicode string. Commented Oct 14, 2014 at 7:31
  • Also, you have a problem with your first if condition; your code is not working as designed even in Python 2. See How do I test one variable against multiple values? Commented Oct 14, 2014 at 7:32
  • You are also reading the each file six times, making your code exceedingly slow. Read the file just once. Commented Oct 14, 2014 at 7:33
  • can someone give me write use of first if statement where i checked 3 conditions Commented Oct 14, 2014 at 8:31

1 Answer 1

1

Code for 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()

A shorter clear code is better. DRY principle: Don't repeat yourself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.