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()
Related
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:
...
How can I insert a string at the beginning of each line in a text file, I have the following code:
f = open('./ampo.txt', 'r+')
with open('./ampo.txt') as infile:
for line in infile:
f.insert(0, 'EDF ')
f.close
I get the following error:
'file' object has no attribute 'insert'
Python comes with batteries included:
import fileinput
import sys
for line in fileinput.input(['./ampo.txt'], inplace=True):
sys.stdout.write('EDF {l}'.format(l=line))
Unlike the solutions already posted, this also preserves file permissions.
You can't modify a file inplace like that. Files do not support insertion. You have to read it all in and then write it all out again.
You can do this line by line if you wish. But in that case you need to write to a temporary file and then replace the original. So, for small enough files, it is just simpler to do it in one go like this:
with open('./ampo.txt', 'r') as f:
lines = f.readlines()
lines = ['EDF '+line for line in lines]
with open('./ampo.txt', 'w') as f:
f.writelines(lines)
Here's a solution where you write to a temporary file and move it into place. You might prefer this version if the file you are rewriting is very large, since it avoids keeping the contents of the file in memory, as versions that involve .read() or .readlines() will. In addition, if there is any error in reading or writing, your original file will be safe:
from shutil import move
from tempfile import NamedTemporaryFile
filename = './ampo.txt'
tmp = NamedTemporaryFile(delete=False)
with open(filename) as finput:
with open(tmp.name, 'w') as ftmp:
for line in finput:
ftmp.write('EDF '+line)
move(tmp.name, filename)
For a file not too big:
with open('./ampo.txt', 'rb+') as f:
x = f.read()
f.seek(0,0)
f.writelines(('EDF ', x.replace('\n','\nEDF ')))
f.truncate()
Note that , IN THEORY, in THIS case (the content is augmented), the f.truncate() may be not really necessary. Because the with statement is supposed to close the file correctly, that is to say, writing an EOF (end of file ) at the end before closing.
That's what I observed on examples.
But I am prudent: I think it's better to put this instruction anyway. For when the content diminishes, the with statement doesn't write an EOF to close correctly the file less far than the preceding initial EOF, hence trailing initial characters remains in the file.
So if the with statement doens't write EOF when the content diminishes, why would it write it when the content augments ?
For a big file, to avoid to put all the content of the file in RAM at once:
import os
def addsomething(filepath, ss):
if filepath.rfind('.') > filepath.rfind(os.sep):
a,_,c = filepath.rpartition('.')
tempi = a + 'temp.' + c
else:
tempi = filepath + 'temp'
with open(filepath, 'rb') as f, open(tempi,'wb') as g:
g.writelines(ss + line for line in f)
os.remove(filepath)
os.rename(tempi,filepath)
addsomething('./ampo.txt','WZE')
f = open('./ampo.txt', 'r')
lines = map(lambda l : 'EDF ' + l, f.readlines())
f.close()
f = open('./ampo.txt', 'w')
map(lambda l : f.write(l), lines)
f.close()
I would like to write to the middle of a line in a file.
for exemple i have a file:
Text.txt:
"i would like to insert information over here >>>>>>>[]<<<<<<<<"
Is is it possible to precise an index where: file.write() has to start writing?
I have started with this:
file = open(file_path, 'w')
file.write()
I think what you can do is to substitute already existing characters with the same amount of other characters you want. You can open a file, locate the starting point, and start writing. But you will overwrite all the following bytes if you use f.write(). If you want to "insert" something inbetween, you have to read and rewrite all the following content of the file.
Overwrite:
with open('text.txt', 'w') as f:
f.write("0123456789")
# now the file 'text.txt' has "0123456789"
with open('text.txt', 'r+b') as f:
f.seek(-4, 2)
f.write(b'a')
# now the file 'text.txt' has "012345a789"
Insert:
with open('text.txt', 'w') as f:
f.write("0123456789")
# now the file 'text.txt' has "0123456789"
with open('text.txt', 'r+b') as f:
f.seek(-4, 2)
the_rest = f.read()
f.seek(-4, 2)
f.write(b'a')
f.write(the_rest)
# now the file 'text.txt' has "012345a6789"
import fileinput
file = [The file where the code is]
for line in fileinput.FileInput(file, inplace=1):
if [The text that should be in that line] in line:
line = line.rstrip()
line = line.replace(line, [The text that should be there after this file was run])
print (line,end="")
As text in that line you should enter the whole line, else it could not work (I didn't test it out though)
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)
I want to open a file, search for a specific word, change the word and save the file again. Sounds really easy - but I just can't get it working... I know that I have to overwrite the whole file but only change this one word!
My Code:
f = open('./myfile', 'r')
linelist = f.readlines()
f.close
for line in linelist:
i =0;
if 'word' in line:
for number in arange(0,1,0.1)):
myNumber = 2 - number
myNumberasString = str(myNumber)
myChangedLine = line.replace('word', myNumberasString)
f2 = open('./myfile', 'w')
f2.write(line)
f2.close
#here I have to do some stuff with these files so there is a reason
#why everything is in this for loop. And I know that it will
#overwrite the file every loop and that is good so. I want that :)
If I make it like this, the 'new' myfile file contains only the changed line. But I want the whole file with the changed line... Can anyone help me?
****EDIT*****
I fixed it! I just turned the loops around and now it works perfectly like this:
f=open('myfile','r')
text = f.readlines()
f.close()
i =0;
for number in arange(0,1,0.1):
fw=open('mynewfile', 'w')
myNumber = 2 - number
myNumberasString = str(myNumber)
for line in text:
if 'word' in line:
line = line.replace('word', myNumberasString)
fw.write(line)
fw.close()
#do my stuff here where I need all these input files
You just need to write out all the other lines as you go. As I said in my comment, I don't know what you are really trying to do with your replace, but here's a slightly simplified version in which we're just replacing all occurrences of 'word' with 'new':
f = open('./myfile', 'r')
linelist = f.readlines()
f.close
# Re-open file here
f2 = open('./myfile', 'w')
for line in linelist:
line = line.replace('word', 'new')
f2.write(line)
f2.close()
Or using contexts:
with open('./myfile', 'r') as f:
lines = f.readlines()
with open('./myfile', 'w') as f:
for line in lines:
line = line.replace('word', 'new')
f.write(line)
Use fileinput passing in whatever you want to replace:
import fileinput
for line in fileinput.input("in.txt",inplace=True):
print(line.replace("whatever","foo"),end="")
You don't seem to be doing anything special in your loop that cannot be calculated first outside the loop, so create the string you want to replace the word with and pass it to replace.
inplace=True will mean the original file is changed. If you want to verify everything looks ok then remove the inplace=True for the first run and you will actually see the replaced output instead of the lines being written to the file.
If you want to write to a temporary file, you can use a NamedTemporaryFile with shutil.move:
from tempfile import NamedTemporaryFile
from shutil import move
with open("in.txt") as f, NamedTemporaryFile(dir=".",delete=False) as out:
for line in f:
out.write(line.replace("whatever","foo"))
move("in.txt",out.name)
One problem you may encounter is matching substrings with replace so if you know the word is always followed in the middle of a sentence surrounded by whitespace you could add that but if not you will need to split and check every word.
from tempfile import NamedTemporaryFile
from shutil import move
from string import punctuation
with open("in.txt") as f, NamedTemporaryFile(dir=".",delete=False) as out:
for line in f:
out.write(" ".join(word if word.strip(punctuation) != "whatever" else "foo"
for word in line.split()))
The are three issues with your current code. First, create the f2 file handle before starting the loop, otherwise you'll overwrite the file in each iteration. Third, you are writing an unmodified line in f2.write(line). I guess you meant f2.write(myChangedLine)? Third, you should add an else statement that writes unmodified lines to the file. So:
f = open('./myfile', 'r')
linelist = f.readlines()
f.close
f2 = open('./myfile', 'w')
for line in linelist:
i =0;
if 'word' in line:
for number in arange(0,1,0.1)):
myNumber = 2 - number
myNumberasString = str(myNumber)
myChangedLine = line.replace('word', myNumberasString)
f2.write(myChangedLine)
else:
f2.write(line)
f2.close()