How to avoid a double serch and replace
Question:
I have the following code:
I need to rem out a line by placing a # at the begining. It works great.
Problem is if the line is already remarked out by a #, and the query is run again. it places a second # in the line. In the actual file "getweather.sh" I end up with a line that looks like. How can I search for the EXACT text?
####home/pi/allsky/get-temp.py
# Read in the file
with open('/home/pi/allsky/getweather.sh', 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace('/home/pi/allsky/get-temp.py', '#/home/pi/allsky/get-temp.py')
# Write the file out again
with open('/home/pi/allsky/getweather.sh', 'w') as file:
file.write(filedata)
Answers:
I think reading in the entire file as a single string is the wrong approach; you can get the behavior you want more easily by calling readlines()
to read in the file as an array of strings (one string per line of text) and handling it on a line-by-line basis instead, like this:
filePath = '/home/pi/allsky/getweather.sh'
# Read all lines of the file into an array of strings
with open(filePath, 'r') as inFile :
inputLines = inFile.readlines()
# Write the array of strings back to the file, with # modification
with open(filePath, 'w') as outFile:
for line in inputLines:
line = line.strip() # remove carriage-return/newline from the end
if (line == '/home/pi/allsky/get-temp.py'):
outFile.write('#')
outFile.write(line)
outFile.write('n')
U can use re module to process string,avoid multi # wrtie to the file.
import re
filedata = re.sub("#.*?/home/pi/allsky/get-temp.py", "#/home/pi/allsky/get-temp.py", filedata)
I have the following code:
I need to rem out a line by placing a # at the begining. It works great.
Problem is if the line is already remarked out by a #, and the query is run again. it places a second # in the line. In the actual file "getweather.sh" I end up with a line that looks like. How can I search for the EXACT text?
####home/pi/allsky/get-temp.py
# Read in the file
with open('/home/pi/allsky/getweather.sh', 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace('/home/pi/allsky/get-temp.py', '#/home/pi/allsky/get-temp.py')
# Write the file out again
with open('/home/pi/allsky/getweather.sh', 'w') as file:
file.write(filedata)
I think reading in the entire file as a single string is the wrong approach; you can get the behavior you want more easily by calling readlines()
to read in the file as an array of strings (one string per line of text) and handling it on a line-by-line basis instead, like this:
filePath = '/home/pi/allsky/getweather.sh'
# Read all lines of the file into an array of strings
with open(filePath, 'r') as inFile :
inputLines = inFile.readlines()
# Write the array of strings back to the file, with # modification
with open(filePath, 'w') as outFile:
for line in inputLines:
line = line.strip() # remove carriage-return/newline from the end
if (line == '/home/pi/allsky/get-temp.py'):
outFile.write('#')
outFile.write(line)
outFile.write('n')
U can use re module to process string,avoid multi # wrtie to the file.
import re
filedata = re.sub("#.*?/home/pi/allsky/get-temp.py", "#/home/pi/allsky/get-temp.py", filedata)