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
  Extracting specific file from an archive tester_V 4 426 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 466 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Reading a file name fron a folder on my desktop Fiona 4 851 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading Specific Rows In a CSV File finndude 3 940 Dec-13-2022, 03:19 PM
Last Post: finndude
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,306 Sep-27-2022, 01:38 PM
Last Post: buran
  Function not executing each file in folder mathew_31 9 2,167 Aug-22-2022, 08:40 PM
Last Post: deanhystad
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,225 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Writing to json file ebolisa 1 970 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,409 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,858 Mar-28-2022, 03:38 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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