The global variable originalInfo contains
Joe;Bloggs;j.bloggs#anemail.com;0715491874;1
I have written a function to delete that line in a text file containing more information of this type. It works, but it is really clunky and inelegant.
f = open("input.txt",'r') # Input file
t = open("output.txt", 'w') #Temp output file
for line in f:
if line != originalInfo:
t.write(line)
f.close()
t.close()
os.remove("input.txt")
os.rename('output.txt', 'input.txt')
Is there a more efficient way of doing this? Thanks
You solution nearly works, but you need to take care of the trailing newline. This is bit shorter version, doing what you intend:
import shutil
with open("input.txt",'r') as fin, open("output.txt", 'w') as fout:
for line in fin:
if line.strip() != originalInfo:
fout.write(line)
shutil.move('output.txt', 'input.txt')
The strip() is a bit extra effort but would strip away extra white space.
Alternatively, you could do:
originalInfo += '\n'
and later in the loop:
if line != originalInfo:
You can open the file, read it by readlines(), close it and open it to write again. With this way you don't have to create an output file:
with open('input.txt') as file:
lines = file.readlines
with open('input.txt') as file:
for line in lines:
if line != originalInfo:
file.write(line)
But if you want to have an output:
with open('input.txt') as input:
with open('output.txt', 'w') as output:
for line in input:
if line != originalInfo:
output.write(line)
Related
so I have a txt file that I am required to add a phrase at every end of the line.
Note that the phrase is the same added on every line
soo what I need is
here are some words
some words are also here
vlavlavlavlavl
blaaablaabalbaaa
before
here are some words, the end
some words are also here, the end
vlavlavlavlavl, the end
blaaablaabalbaaa, the end
after
i also tried this method
with open("Extracts.txt", encoding="utf-8") as f:
for line in f:
data = [line for line in f]
with open("new.txt", 'w', encoding="utf-8") as f:
for line in data:
f.write(", Deposited")
f.write(line)
but the word was shown at the beginning of the line and not the end.
line ends with a newline. Remove the newline, write the line and the addition, followed by a newline.
There's also no need to read the lines into a list first, you can just iterate over the input file directly.
with open("Extracts.txt", encoding="utf-8") as infile, open("new.txt", 'w', encoding="utf-8") as outfile:
for line in infile:
line = line.rstrip("\n")
outfile.write(f"{line}, Deposited\n")
You can first get all the lines in the text file using the readlines method, and then add the line you want to.
with open("Extracts.txt", encoding="utf-8") as f:
data = f.readlines()
new_data = []
for line in data:
line = line.replace("\n", "")
line += " , Deposited\n"
new_data.append(line)
with open("new.txt", "w", encoding="utf-8") as f:
f.writelines(new_data)
As mkrieger1 already said, the order of operations here is wrong. You are writing the ", Deposited" to the file before you're writing the content of the line in question. So a working version of the code swaps those operations:
with open("Extracts.txt", encoding="utf-8") as f:
for line in f:
data = [line for line in f]
with open("new.txt", 'w', encoding="utf-8") as f:
for line in data:
f.write(line.strip())
f.write(", Deposited\n")
Note that I also added a strip() function to handling the line of text, this removes whitespaces at the start and end of the string to get rid of any extra line changes before the ", Deposited". Then the line change was manually added to the end of the string as a string literal "\n".
I'm trying to remove one line which matches a variable. But instead it is wiping the file clean.
a_file = open("./Variables/TxtFile.txt", "r")
lines = a_file.readlines()
a_file.close()
new_file = open("./Variables/TxtFile.txt", "w")
for line in lines:
if line.strip("\n") == VariableStore:
new_file.write(line)
new_file.close()
The goal would be to remove the line that matches VariableStore rather than wiping the entire text file
In regard to my comment to your original post.
You only write to the file if you match the line you want to remove and then also close the file.
This seems not to be what you want.
You might want to change the if condition to be executed in cases that do not match your line you want to remove, i.e., to if not line.strip("\n") == VariableStore: and close the file after your loop, i.e., on the same level as your for loop.
Try the following, which incorporates these suggestions:
a_file = open("./Variables/TxtFile.txt", "r")
lines = a_file.readlines()
a_file.close()
new_file = open("./Variables/TxtFile.txt", "w")
for line in lines:
if not line.strip("\n") == VariableStore:
new_file.write(line)
new_file.close()
If your aim is to filter out the line matching VariableStore, do this:
with open("./Variables/TxtFile.txt", "r") as a_file:
lines = a_file.readlines()
with open("./Variables/TxtFile.txt", "w") as new_file:
for line in lines:
if line.strip("\n") != VariableStore:
continue # Skip the VariableStore line
new_file.write(line) # Write other lines
When you use with statements, you don't need to manually close the file.
You just need to close the file later on, when you are done parsing all the lines.
Also, you need to write the lines that don't match, not the one's that do.
Note the changes below:
# Read file
a_file = open("./Variables/TxtFile.txt", "r")
lines = a_file.readlines()
a_file.close()
# Write file
new_file = open("./Variables/TxtFile.txt", "w")
for line in lines:
if line.strip("\n") == VariableStore:
# Don't write this line
pass
else:
new_file.write(line)
new_file.close()
Let us assume that your text file TxtFile.txt contains this text
Hello
World
I'm
Python
Developer
And you have a variable var contains the string World which we want to remove from the text file.
Here is a python code does the job in few lines
var='World' # a string to remove
with open("TxtFile.txt","r+") as f:
lines = f.readlines()
lines = [line for line in lines if line.strip()!=var]
f.seek(0)
f.writelines(lines)
f.truncate()
The text file after running this code..
Hello
I'm
Python
Developer
The problem is that you're opening the file with write mode instead of append mode. Replace
new_file = open("./Variables/TxtFile.txt", "w")
with
new_file = open("./Variables/TxtFile.txt", "a")
and you'll append the data instead of overwriting it.
Also, it's generally recommended to open files using the 'with' statement, since that automatically closes the file for you.
with open("./Variables/TxtFile.txt", "a") as text_file:
...
I am trying to do what for many will be a very straight forward thing but for me is just infuriatingly difficult.
I am trying search for a line in a file that contains certain words or phrases and modify that line...that's it.
I have been through the forum and suggested similar questions and have found many hints but none do just quite what I want or are beyond my current ability to grasp.
This is the test file:
# 1st_word 2nd_word
# 3rd_word 4th_word
And this is my script so far:
############################################################
file = 'C:\lpthw\\text'
f1 = open(file, "r+")
f2 = open(file, "r+")
############################################################
def wrline():
lines = f1.readlines()
for line in lines:
if "1st_word" in line and "2nd_word" in line:
#f2.write(line.replace('#\t', '\t'))
f2.write((line.replace('#\t', '\t')).rstrip())
f1.seek(0)
wrline()
My problem is that the below inserts a \n after the line every time and adds a blank line to the file.
f2.write(line.replace('#\t', '\t'))
The file becomes:
1st_word 2nd_word
# 3rd_word 4th_word
An extra blank line between the lines of text.
If I use the following:
f2.write((line.replace('#\t', '\t')).rstrip())
I get this:
1st_word 2nd_wordd
# 3rd_word 4th_word
No new blank line inserted but and extra "d" at the end instead.
What am I doing wrong?
Thanks
Your blank line is coming from the original blank line in the file. Writing a line with nothing in it writes a newline to the file. Instead of not putting anything into the written line, you have to completely skip the iteration, so it does not write that newline. Here's what I suggest:
def wrline():
lines = open('file.txt', 'r').readlines()
f2 = open('file.txt', 'w')
for line in lines:
if '1st_word' in line and '2nd_word' in line:
f2.write((line.replace('# ', ' ')).rstrip('\n'))
else:
if line != '\n':
f2.write(line)
f2.close()
I would keep read and write operations separate.
#read
with open(file, 'r') as f:
lines = f.readlines()
#parse, change and write back
with open(file, 'w') as f:
for line in lines:
if line.startswith('#\t'):
line = line[1:]
f.write(line)
You have not closed the files and there is no need for the \t
Also get rid of the rstrip()
Read in the file, replace the data and write it back.. open and close each time.
fn = 'example.txt'
new_data = []
# Read in the file
with open(fn, 'r+') as file:
filedata = file.readlines()
# Replace the target string
for line in filedata:
if "1st_word" in line and "2nd_word" in line:
line = line.replace('#', '')
new_data.append(line)
# Write the file out again
with open(fn, 'w+') as file:
for line in new_data:
file.write(line)
I have a text file that looks like this
Big:house
small:door
Big:car
Small:chair
Big:plane
How to I remove the lines that contain the word "big" so it may look like this, I dont want to create a new file all together though
small:door
small:chair
Here was my attempt
with open('QWAS.txt','r') as oldfile:
for line in oldfile:
if bad_words in line:
newfile.write(line)
This is what we can do:
Read data to string (remove rows that start with 'big')
Go to the start of file (seek)
Write the string
Truncate (remove overflow)
And now to the code, open it in read and write mode:
with open('QWAS.txt','r+') as f:
data = ''.join([i for i in f if not i.lower().startswith('big')]) #1
f.seek(0) #2
f.write(data) #3
f.truncate() #4
Try this:
newfile = r'output.txt'
oldfile = r'input.txt'
with open(newfile, 'w') as outfile, open(oldfile, 'r') as infile:
for line in infile:
if if line[:5].lower() == 'small':
outfile.write(line)
#output
small:door
Small:chair
Of course, this assumes you want to eliminate rows where small or Small is to the left of the colon. Additionally, you will have a new file output, as I don't think you really want to update your input file.
You can try using regular expressions
import re
oldfile = open('QWAS.txt','r')
newfile = open('newfile.txt','w')
for line in oldfile:
if re.search('[Ss]mall',line):
newfile.write(line)
oldfile.close()
newfile.close()
Which gives the output file "newfile.txt"
small:door
Small:chair
If you just take every line that doesn't have small and write it to a new file "newfile2.txt"
import re
oldfile = open('QWAS.txt','r')
newfile = open('newfile.txt','w')
newfile2 = open('newfile2.txt','w')
for line in oldfile:
if re.search('[Ss]mall',line):
newfile.write(line)
else:
newfile2.write(line)
oldfile.close()
newfile.close()
newfile2.close()
How could I print the final line of a text file read in with python?
fi=open(inputFile,"r")
for line in fi:
#go to last line and print it
One option is to use file.readlines():
f1 = open(inputFile, "r")
last_line = f1.readlines()[-1]
f1.close()
If you don't need the file after, though, it is recommended to use contexts using with, so that the file is automatically closed after:
with open(inputFile, "r") as f1:
last_line = f1.readlines()[-1]
Do you need to be efficient by not reading all the lines into memory at once? Instead you can iterate over the file object.
with open(inputfile, "r") as f:
for line in f: pass
print line #this is the last line of the file
Three ways to read the last line of a file:
For a small file, read the entire file into memory
with open("file.txt") as file:
lines = file.readlines()
print(lines[-1])
For a big file, read line by line and print the last line
with open("file.txt") as file:
for line in file:
pass
print(line)
For efficient approach, go directly to the last line
import os
with open("file.txt", "rb") as file:
# Go to the end of the file before the last break-line
file.seek(-2, os.SEEK_END)
# Keep reading backward until you find the next break-line
while file.read(1) != b'\n':
file.seek(-2, os.SEEK_CUR)
print(file.readline().decode())
If you can afford to read the entire file in memory(if the filesize is considerably less than the total memory), you can use the readlines() method as mentioned in one of the other answers, but if the filesize is large, the best way to do it is:
fi=open(inputFile, 'r')
lastline = ""
for line in fi:
lastline = line
print lastline
You could use csv.reader() to read your file as a list and print the last line.
Cons: This method allocates a new variable (not an ideal memory-saver for very large files).
Pros: List lookups take O(1) time, and you can easily manipulate a list if you happen to want to modify your inputFile, as well as read the final line.
import csv
lis = list(csv.reader(open(inputFile)))
print lis[-1] # prints final line as a list of strings
If you care about memory this should help you.
last_line = ''
with open(inputfile, "r") as f:
f.seek(-2, os.SEEK_END) # -2 because last character is likely \n
cur_char = f.read(1)
while cur_char != '\n':
last_line = cur_char + last_line
f.seek(-2, os.SEEK_CUR)
cur_char = f.read(1)
print last_line
This might help you.
class FileRead(object):
def __init__(self, file_to_read=None,file_open_mode=None,stream_size=100):
super(FileRead, self).__init__()
self.file_to_read = file_to_read
self.file_to_write='test.txt'
self.file_mode=file_open_mode
self.stream_size=stream_size
def file_read(self):
try:
with open(self.file_to_read,self.file_mode) as file_context:
contents=file_context.read(self.stream_size)
while len(contents)>0:
yield contents
contents=file_context.read(self.stream_size)
except Exception as e:
if type(e).__name__=='IOError':
output="You have a file input/output error {}".format(e.args[1])
raise Exception (output)
else:
output="You have a file error {} {} ".format(file_context.name,e.args)
raise Exception (output)
b=FileRead("read.txt",'r')
contents=b.file_read()
lastline = ""
for content in contents:
# print '-------'
lastline = content
print lastline
I use the pandas module for its convenience (often to extract the last value).
Here is the example for the last row:
import pandas as pd
df = pd.read_csv('inputFile.csv')
last_value = df.iloc[-1]
The return is a pandas Series of the last row.
The advantage of this is that you also get the entire contents as a pandas DataFrame.