Writing to file in a specific folder - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Writing to file in a specific folder (/thread-30926.html) |
Writing to file in a specific folder - evapa8f - Nov-13-2020 Having trouble writing to a file in a specific directory. I'm using a loop to capture info from multiple devices so the actual filename is represented as a the variable "file" file = (hostname + '.txt') file = file.lstrip("\r\n") s = open('\\home\\verizon\\backups\\Cisco\\'%file ,'w') s.write(output) s.close() RE: Writing to file in a specific folder - Axel_Erfurt - Nov-13-2020 s = open(f'/home/verizon/backups/Cisco/{file}' ,'w') RE: Writing to file in a specific folder - evapa8f - Nov-13-2020 (Nov-13-2020, 05:03 PM)Axel_Erfurt Wrote:s = open(f'/home/verizon/backups/Cisco/{file}' ,'w') getting this error Carrot belongs under the second quote (after {file})
RE: Writing to file in a specific folder - bowlofred - Nov-13-2020 F-strings (which this example uses) were introduced in python 3.6. You'll get a syntax error in earlier versions. What version are you using? Can you use a more recent copy? RE: Writing to file in a specific folder - Axel_Erfurt - Nov-13-2020 For older version use s = open("%s%s" % ('/home/verizon/backups/Cisco/', file), 'w') RE: Writing to file in a specific folder - deanhystad - Nov-13-2020 Should use os.path or pathlib. |