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 to file ends incorrectly project_science 4 202 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 186 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Find frequencies of an audio file at a specific time via librosa jberesford123__ 0 168 Oct-21-2020, 01:18 PM
Last Post: jberesford123__
  Extracting data based on specific patterns in a text file K11 1 403 Aug-28-2020, 09:00 AM
Last Post: Gribouillis
  Find specific subdir, open files and find specific lines that are missing from a file tester_V 8 759 Aug-25-2020, 01:52 AM
Last Post: tester_V
  Python Cut/Copy paste file from folder to another folder rdDrp 4 567 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  Failure in writing binary text to file Gigux 7 739 Jul-04-2020, 08:41 AM
Last Post: Gigux
  writing data to a csv-file apollo 1 364 Jul-03-2020, 02:28 PM
Last Post: DeaD_EyE
  Process Data from one csv file and write to another CSV file specific column ajin9581 1 431 Jun-17-2020, 06:00 PM
Last Post: buran
  Writing to File Issue Flash_Stang 3 473 Jun-05-2020, 05:14 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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