I need to remove the following last two lines from my textfile:
ColorForeground=#000000
ColorBackground=#ffffff
The above lines are appended to the terminalrc file with the following command:
echo -e "ColorForeground=#000000\nColorBackground=#ffffff">>/home/jerzy/.config/xfce4/terminal/terminalrc
Thus, the last lines of the file to be modified look like this
DropdownKeepOpenDefault=TRUE
ColorForeground=#000000
ColorBackground=#ffffff
I wrote the following Python script in order to remove the last two lines of the file with .replace() method:
day = r"ColorForeground=#000000\nColorBackground=#ffffff"
file = r"/home/jerzy/.config/xfce4/terminal/terminalrc"
with open(file) as f:
content = f.read()
content = content.replace(day, "")
with open(file, 'r+') as f2:
f2.write(content)
Yet, my script does not work as expected. The following is the result of its execution:
DropdownKeepOpenDefault=TRUE
olorForeground=#000000
ColorBackground=#ffffff
Where is the error in my Python code? How would you write such a script? Is this task doable without using regular expressions?
Read and write seperately, also don't make day a raw string, that will escape the newline-
day = "ColorForeground=#000000\nColorBackground=#ffffff\n"
with open(file, 'r') as f:
content = f.read()
content = content.replace(day, "")
with open(file, 'w') as f:
f.write(content)
Related
Whats the way to extract only lines with specific word only from requests (online text file) and write to a new text file? I am stuck here...
This is my code:
r = requests.get('http://website.com/file.txt'.format(x))
with open('data.txt', 'a') as f:
if 'word' in line:
f.write('\n')
f.writelines(str(r.text))
f.write('\n')
If I remove: if 'word' in line:, it works, but for all lines. So it's only copying all lines from one file to another.
Any idea how to give the correct command to extract (filter) only lines with specific word?
Update: This is working but If that word exist in the requests file, it start copying ALL lines, i need to copy only the line with 'SOME WORD'.
I have added this code:
for line in r.text.split('\n'):
if 'SOME WORD' in line:
*Thank you guys for all the answers and sorry If i didn't made myself clear.
Perhaps this will help.
Whenever you invoke POST/GET or whatever, always check the HTTP response code.
Now let's assume that the lines within the response text are delimited with newline ('\n') and that you want to write a new file (change the mode to 'a' if you want to append). Then:
import requests
(r := requests.get('SOME URL')).raise_for_status()
with open('SOME FILENAME', 'w') as outfile:
for line in r.text.split('\n'):
if 'SOME WORD' in line:
print(line, file=outfile)
break
Note:
You will need Python 3.8+ in order to take advantage of the walrus operator in this code
I would suggest you these steps for properly handling the file:
Step1:Streamline the download file to a temporary file
Step2:Read lines from the temporary file
Step3:Generate main file based on your filter
Step4:Delete the temporary file
Below is the code that does the following steps:
import requests
import os
def read_lines(file_name):
with open(file_name,'r') as fp:
for line in fp:
yield line
if __name__=="__main__":
word='ipsum'
temp_file='temp_file.txt'
main_file='main_file.txt'
url = 'https://filesamples.com/samples/document/txt/sample3.txt'
with open (temp_file,'wb') as out_file:
content = requests.get(url, stream=True).content
out_file.write(content)
with open(main_file,'w') as mf:
out=filter(lambda x: word in x,read_lines(temp_file))
for i in out:
mf.write(i)
os.remove(temp_file)
Well , there is missing line you have to put in order to check with if statement.
import requests
r = requests.get('http://website.com/file.txt').text
with open('data.txt', 'a') as f:
for line in r.splitlines(): #this is your loop where you get a hold of line.
if 'word' in line: #so that you can check your 'word'
f.write(line) # write your line contains your word
This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 3 years ago.
I am trying to display my python file in html and therefore I would like to replace every time the file jumps to a newline with < br> but the program I've written is not working.
I've looked on here and tried changing the code around a bit I have gotten different results but not the ones I need.
with open(path, "r+") as file:
contents = file.read()
contents.replace("\n", "<br>")
print(contents)
file.close()
I want to have the file display < br> every time I have a new line but instead the code dosen't change anything to the file.
Here is an example program that works:
path = "example"
contents = ""
with open(path, "r") as file:
contents = file.read()
new_contents = contents.replace("\n", "<br>")
with open(path, "w") as file:
file.write(new_contents)
Your program doesn't work because the replace method does not modify the original string; it returns a new string.
Also, you need to write the new string to the file; python won't do it automatically.
Hope this helps :)
P.S. a with statement automatically closes the file stream.
Your code reads from the file, saves the contents to a variable and replaces the newlines. But the result is not saved anywhere. And to write the result into a file you must open the file for writing.
with open(path, "r+") as file:
contents = file.read()
contents = contents.replace("\n", "<br>")
with open(path, "w+") as file:
contents = file.write(contents)
there are some issues in this code snippet.
contents.replace("\n", "<br>") will return a new object which replaced \n with <br>, so you can use html_contents = contents.replace("\n", "<br>") and print(html_contents)
when you use with the file descriptor will close after leave the indented block.
Try this:
import re
with open(path, "r") as f:
contents = f.read()
contents = re.sub("\n", "<br>", contents)
print(contents)
Borrowed from this post:
import tempfile
def modify_file(filename):
#Create temporary file read/write
t = tempfile.NamedTemporaryFile(mode="r+")
#Open input file read-only
i = open(filename, 'r')
#Copy input file to temporary file, modifying as we go
for line in i:
t.write(line.rstrip()+"\n")
i.close() #Close input file
t.seek(0) #Rewind temporary file to beginning
o = open(filename, "w") #Reopen input file writable
#Overwriting original file with temporary file contents
for line in t:
o.write(line)
t.close() #Close temporary file, will cause it to be deleted
I am reading a text file.
One line from the text file looks like this and comes at the very end of the text file:
</DTS:Executable>
I am using replace("</DTS:Executable>","test from me")
Nothing gets replaced and the text stays as is.
What am I doing wrong?
What is the extension of the file ?
Can you try this sed command :
sed -i 's/original/new/g' file.txt
How about reading the data in list and replacing the last element, like so;
with open(fname) as f:
content = f.readlines()
lines = [line.rstrip('\n') for line in file]
lines[-1] = "test from me"
Did this work?
If the file content are not to large then straight forward you can do something like this
with open(filename) as f:
content = f.read()
content = content.replace("<old>", "<new>")
with open(filename, "w") as f:
f.write(content)
I am pretty new to python and have only very limited programming skills with it. I hope you can help me here.
I have a large text file and I'm searching it for a specific word. Every line with this word needs to be stored to another txt file.
I can search the file and print the result in the console but not to a different file. How can I manage that?
f = open("/tmp/LostShots/LostShots.txt", "r")
searchlines = f.readlines()
f.close()
for i, line in enumerate(searchlines):
if "Lost" in line:
for l in searchlines[i:i+3]: print l,
print
f.close()
Thx
Jan
Use with context manager, do not use readlines() since it will read the whole contents of a file into a list. Instead iterate over file object line by line and see if a specific word is there; if yes - write to the output file:
with open("/tmp/LostShots/LostShots.txt", "r") as input_file, \
open('results.txt', 'w') as output_file:
for line in input_file:
if "Lost" in line:
output_file.write(line)
Note that for python < 2.7, you cannot have multiple items in with:
with open("/tmp/LostShots/LostShots.txt", "r") as input_file:
with open('results.txt', 'w') as output_file:
for line in input_file:
if "Lost" in line:
output_file.write(line)
To correctly match words in general, you need regular expressions; a simple word in line check also matches blablaLostblabla which I assume you don't want:
import re
with open("/tmp/LostShots/LostShots.txt", "r") as input_file, \
open('results.txt', 'w') as output_file:
output_file.writelines(line for line in input_file
if re.match(r'.*\bLost\b', line)
or you can use a more wordy
for line in input_file:
if re.match(r'.*\bLost\b', line)):
output_file.write(line)
As a side note, you should be using os.path.join to make paths; also, for working with temporary files in a cross-platform manner, see the functions in the tempfile module.
I am trying to form a quotes file of a specific user name in a log file. How do I remove every line that does not contain the specific user name in it? Or how do I write all the lines which contain this user name to a new file?
with open('input.txt', 'r') as rfp:
with open('output.txt', 'w') as wfp:
for line in rfp:
if ilikethis(line):
wfp.write(line)
with open(logfile) as f_in:
lines = [l for l in f_in if username in l]
with open(outfile, 'w') as f_out:
f_out.writelines(lines)
Or if you don't want to store all the lines in memory
with open(logfile) as f_in:
lines = (l for l in f_in if username in l)
with open(outfile, 'w') as f_out:
f_out.writelines(lines)
I sort of like the first one better but for a large file, it might drag.
Something along this line should suffice:
newfile = open(newfilename, 'w')
for line in file(filename, 'r'):
if name in line:
newfile.write(line)
newfile.close()
See : http://docs.python.org/tutorial/inputoutput.html#methods-of-file-objects
f.readlines() returns a list containing all the lines of data in the file.
An alternative approach to reading lines is to loop over the file object. This is memory efficient, fast, and leads to simpler code
>>> for line in f:
print line
Also you can checkout the use of with keyword. The advantage that the file is properly closed after its suite finishes
>>> with open(filename, 'r') as f:
... read_data = f.read()
>>> f.closed
True
I know you asked for python, but if you're on unix this is a job for grep.
grep name file
If you're not on unix, well... the answer above does the trick :)