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()
Related
I want to replace the first character in each line from the text file.
2 1.510932 0.442072 0.978141 0.872182
5 1.510932 0.442077 0.978141 0.872181
Above is my text file.
import sys
import glob
import os.path
list_of_files = glob.glob('/path/txt/23.txt')
for file_name in list_of_files:
f= open(file_name, 'r')
lst = []
for line in f:
f = open(file_name , 'w')
if line.startswith("2 "):
line = line.replace("2 ","7")
f.write(line)
f.close()
What i want:-
If the number starting with 2, i want to change that into 7. The problem is that, In the same line multiple 7 is there. If i change startswith character and save everything was changing
Thanks
The proper solution is (pseudo code):
open sourcefile for reading as input
open temporaryfile for writing as output
for each line in input:
fix the line
write it to output
close input
close output
replace sourcefile with temporaryfile
We use a temporary file and write along to avoid potential memory errors.
I leave it up to you to translate this to Python (hint: that's quite straightforward).
This is one approach.
Ex:
for file_name in list_of_files:
data = []
with open(file_name) as infile:
for line in infile:
if line.startswith("2 "): #Check line
line = " ".join(['7'] + line.split()[1:]) #Update line
data.append(line)
with open(file_name, "w") as outfile: #Write back to file
for line in data:
outfile.write(line+"\n")
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()
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()
More details below:
1st line
2nd line
3rd line
4th line
...
Now want to insert a new line named zero line before 1st line. File looks like below:
zero line
1st line
2nd line
3rd line
4th line
...
I know sed command can do this work, but how to do it using python? Thanks
you can use fileinput
>>> import fileinput
>>> for linenum,line in enumerate( fileinput.FileInput("file",inplace=1) ):
... if linenum==0 :
... print "new line"
... print line.rstrip()
... else:
... print line.rstrip()
...
this might be of interest
http://net4geeks.com/index.php?option=com_content&task=view&id=53&Itemid=11
adapted to your question:
# read the current contents of the file
f = open('filename')
text = f.read()
f.close()
# open the file again for writing
f = open('filename', 'w')
f.write("zero line\n\n")
# write the original contents
f.write(text)
f.close()
Open the file and read the
contents into 'text'.
Close the file
Reopen the file with argument 'w' to
write
Write text to prepend to the file
Write the original contents of the
file to the file
Close file
Read the warnings in the link.
edit:
But note that this isn't entirely
safe, if your Python session crashes
after opening the file the second time
and before closing it again, you will
lose data.
Here's an implementation that fixes some deficiencies in other approaches presented sofar:
it doesn't lose data in case of an error — #kriegar's version does
supports empty files — fileinput version does not
preserves original data: doesn't mangle trailing whitespace — fileinput version does
and does not read the whole file in memory as the version from net4geeks.com does.
It mimics fileinput's error handling:
import os
def prepend(filename, data, bufsize=1<<15):
# backup the file
backupname = filename + os.extsep+'bak'
try: os.unlink(backupname) # remove previous backup if it exists
except OSError: pass
os.rename(filename, backupname)
# open input/output files, note: outputfile's permissions lost
with open(backupname) as inputfile, open(filename, 'w') as outputfile:
# prepend
outputfile.write(data)
# copy the rest
buf = inputfile.read(bufsize)
while buf:
outputfile.write(buf)
buf = inputfile.read(bufsize)
# remove backup on success
try: os.unlink(backupname)
except OSError: pass
prepend('file', '0 line\n')
You could use cat utility if it is available to copy the files. It might be more efficient:
import os
from subprocess import PIPE, Popen
def prepend_cat(filename, data, bufsize=1<<15):
# backup the file
backupname = filename + os.extsep+'bak'
try: os.unlink(backupname)
except OSError: pass
os.rename(filename, backupname)
# $ echo $data | cat - $backupname > $filename
with open(filename, 'w') as outputfile: #note: outputfile's permissions lost
p = Popen(['cat', '-', backupname], stdin=PIPE, stdout=outputfile)
p.communicate(data)
# remove backup on success
if p.poll() == 0:
try: os.unlink(backupname)
except OSError: pass
prepend_cat('file', '0 line\n')
with open(filename, 'r+') as f:
lines = f.readlines()
lines.insert(0, 'zero line\n')
f.seek(0)
f.writelines(lines)
code
L = list()
f = open('text2.txt', 'r')
for line in f.readlines():
L.append(line)
L.insert(0,"Zero\n")
f.close()
fi = open('text2.txt', 'w')
for line in xrange(len(L)):
fi.write(L[line])
fi.close()
text2.txt
Hello
The second line
3
4
5
6
output
Zero
Hello
The second line
3
4
5
6
This can be memory heavy and time consuming for large files however.
If you are worried about something like 31st line, I would just do a mod%10 on the num, to get a more accurate version.
Let me know if this helps, or if you want a better version. Also, if you want a better formatting, look into ljust and rjust for left and right justify.