I have the following code, it will create the logfile on both platforms, but it will only write the output to the windows logfile.
import os
logdir = os.getenv("logs")
logfile = 'script_list.log'
path = (raw_input("Enter dir: "))
if os.name == "posix":
log = open("//".join([logdir,logfile]), 'w')
else:
log = open("\\".join([logdir,logfile]), 'w')
for dirpath, dirname, filenames in os.walk(path):
for filename in [f for f in filenames]:
if os.name == "posix":
log.write(str(dirpath)+ "//" + str(filename) + "\n")
else:
log.write(str(dirpath)+ "\\" + str(filename) + "\n")
print '\nYour logfile ' + logdir + '\\' + logfile + ' has been created'
Thanks in advance
I suggest you replace
with
Or even better, use
As @Lycha says, it’s a good idea to close your file again. So your construct would be