1
votes

What's wrong with this Python file-read test?

import os
import sys

fileList = os.listdir(sys.argv[1])
count = 0
for file in fileList:
    try:
        count += 1
        print os.path.isfile(file)
        if os.path.isfile(file)
            print "File >> " + file
        else
            print "Dir >> " + file
    except err:
        print "ERROR: " + err
        print ">> in file: " + file

gives:

File "test.py", line 10 if os.path.isfile(file) ^ SyntaxError: invalid syntax

why?

1
Also, enumerate. - user395760
@delnan what's the difference of what I'm doing? - The Student
The enumerate function returns a enumerator object which is a list of tuples in the format (index, object), so list(enumerate(["a", "b"])) returns [(0, 'a'), (1, 'b')]. So you would need to use the count variable. You could use for count, file in enumerate(fileList): - None
@None looks more complicated than what I just did. Actually, if you're talking about, so it must be better someway, but I'm not understanding in which way it's better.. - The Student
It's better in that it's two lines less and plays nicely with continue. It's also a matter of using the existing solution instead of doing it yourself. And how is this "more complicated"? - user395760

1 Answers

5
votes

missing colon on if and else lines

if (something):   #note the : at the end
  ...
else:
  ...