Python Forum
Writing to file in a specific folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing to file in a specific folder
#1
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()
buran write Nov-13-2020, 08:02 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
s = open(f'/home/verizon/backups/Cisco/{file}' ,'w')
Reply
#3
(Nov-13-2020, 05:03 PM)Axel_Erfurt Wrote:
s = open(f'/home/verizon/backups/Cisco/{file}' ,'w')

getting this error

Error:
[verizon@lab-gov-tac python]$ python iosbackup.py File "iosbackup.py", line 73 s = open(f'/home/verizon/backups/Cisco/{file}' , 'w') ^ SyntaxError: invalid syntax [verizon@lab-gov-tac python]$
Carrot belongs under the second quote (after {file})
buran write Nov-13-2020, 08:03 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#4
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?
Reply
#5
For older version use

s = open("%s%s" % ('/home/verizon/backups/Cisco/', file), 'w')
Reply
#6
Should use os.path or pathlib.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  writing list to csv file problem jacksfrustration 5 1,330 Jul-04-2024, 08:15 PM
Last Post: deanhystad
  Delete file with read-only permission, but write permission to parent folder cubei 6 23,776 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  Extracting specific file from an archive tester_V 4 1,190 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 1,142 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Reading a file name fron a folder on my desktop Fiona 4 1,605 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading Specific Rows In a CSV File finndude 3 1,522 Dec-13-2022, 03:19 PM
Last Post: finndude
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 2,610 Sep-27-2022, 01:38 PM
Last Post: buran
  Function not executing each file in folder mathew_31 9 3,271 Aug-22-2022, 08:40 PM
Last Post: deanhystad
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,770 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Writing to json file ebolisa 1 1,373 Jul-17-2022, 04:51 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020