I can’t save this output, maybe someone have the solution. I’m listing a directory and some singles files. But when I save the output just catch the directory files, and Not the singles files.
My code:
import os
tosave = open('/tmp/list','ab')
thesource = ["/etc/ssh","/var/log/syslog","/etc/hosts"]
for f in thesource:
print f
for top, dirs, files in os.walk(f):
for nm in files:
print os.path.join(top, nm)
try:
tosave.write(top+nm+'\n')
finally:
tosave.close
I saw in the console all files and directory, but in the saved list, just ssh files. Why didn’t save syslog and hosts too?
Thank you !!
In case you missed the
()attosave.closewhile pasting: (otherwise check harsh’s answer)The
finallyis wrong here. The code infinallywill be executed after thetryblock, so after the first execution oftosave.write(top+nm+'\n')the file will be closed because oftosave.close().Possibly you intended to use
except:Edit: To answer your comment, you want the last line to be the same as the print statement: