Use variable inside open and also use find and replace in python [on hold]
I have a text file backup.txt It contains =========================== today's date is day =========================== I want to replace day with day variable here is the code tmp=/tmp day = datetime.date.today().day print (day) f1 = open('"%s/backup.txt" % (tmp)', 'r') f2 = open('"%s/backup1.txt" % (tmp)', 'w') for line in f1: f2.write(line.replace('day', '"%s" % (day)')) f1.close() f2.close() It throws me: IOError: [Errno 2] No such file or directory: '"%s/backup.txt" % (tmp)' /tmp defined above is not getting replaced. Can you someone please advice?
Syntax error at: for line in f1: f2.write(line.replace('day', '"%s" % (day)') a ) is missing, it should actually be: f2.write(line.replace('day', '"%s" % (day)'))
Python: Trouble with dill installation
python open file error
Dealing with Sprites and Collisions Using Pygame
Pip-installed module does not import
Does a trailing comma after an n-tuple in Python change its value?
Program that either waits for user input or runs at defined intervals?
pexpect for passwordless access does not work
Kivy Widget does not accepts properties and commands
Efficient combinations of combinations
what metric should I monitor for a spider in scrapy
Why is Aptana/Pydev showing “undefined variable” errors for “print” and “__name__” in my helloworld python code?
Python Tkinter: Delete label not working
How to call a function after adding values to an Option Menu?
using python to verify that a shapefile is a shapefile (fiona, ogr)
Identifying largest connected component in a matrix
VBA shell script set environment variable before execution