python


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)'))

Related Links

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

Categories

HOME
makefile
swift
laravel-5
sql-server
udp
itext
system-verilog
blockchain
webrtc
antivirus
visual-studio-2013
facebook-graph-api
google-contacts
translation
mapbox-gl-js
amazon-product-api
mainframe
chaiscript
couchdb-2.0
appmaker
tizen-wearable-sdk
tumblr
jpanel
internet-explorer-11
dropbear
android-viewpager
flexbox
ndis
sign
lstm
sensu
hhvm
memsql
bcrypt
tflearn
badge
svnkit
rhino
shibboleth
xamarin.uitest
readfile
jquery-multidatespicker
resuming-training
aqgridview
nstouchbar
livefyre
mouseclick-event
chrome-remote-desktop
etherpad
httplib2
master
backstop.js
windows-azure-pack
glkit
firepath
pax-web
com-interop
fill
cmocka
loopj
servlet-3.0
test-class
cctray
java.util.calendar
enyo
code-readability
matcaffe
retro-computing
built-in
bridge
pylearn
uos
jython-2.7
multipleselection
pl-i
jcr-sql2
kallithea
named-ranges
scala-2.11
android-json-rpc
magicalrecord-2.2
confusion-matrix
installshield-2011
va-list
sharpmap
resgen
soa-suite
template-haskell
tridion2009
mvs
cgpath
git-filter-branch
mvccontrib
radchart
pstree
wcf-web-api
hibernate3
file-exists
msgbox
selectmanycheckbox
dcpu-16
smooth
sitemappath
visual-c++-2008-express
savestate
visual-studio-2010-beta-2
evb
weborb
data-execution-prevention
divx

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App