I have this simple code here
import os
path = (raw_input("Enter dir: "))
f = open('script_list.log', 'w')
for dirpath, dirname, filenames in os.walk(path):
for filename in [f for f in filenames]:
f.write(str(filename) + "\n")
print os.path.join(dirpath, filename)
When I run it I am getting the following
Enter dir: scripts
Traceback (most recent call last):
File "C:\Documents and Settings\CRichards\My Documents\My Dropbox\this_code.py", line 8, in <module>
f.write(str(filename) + "\n")
AttributeError: 'str' object has no attribute 'write'
I know it must be something simple, I just can’t see it.
You’re rebinding
fin the loop when you do[f for f in filenames]. When you get to the point wheref.writeis called,fis the last member offilenames, so it’s a string. Rename the outerfto something likelogoroutput, or better, get rid of the useless list comprehension:suffices.
(List comprehensions don’t introduce a new scope.)