Replace line in file contents

How to open a file, Stud.txt, and then replace any occurrences of "A" with "Orange"?

+85
python string file-io
Nov 08 2018-10-21
source share
7 answers
with open("Stud.txt", "rt") as fin: with open("out.txt", "wt") as fout: for line in fin: fout.write(line.replace('A', 'Orange')) 
+164
Nov 08 2018-10-21
source share

If you want to replace the lines in the same file, you probably have to read its contents into a local variable, close it and open it again for writing:

In this example, I use the with statement , which closes the file after the with block completes - usually after the last command completes, or by exception.

 def inplace_change(filename, old_string, new_string): # Safely read the input filename using 'with' with open(filename) as f: s = f.read() if old_string not in s: print('"{old_string}" not found in {filename}.'.format(**locals())) return # Safely write the changed content, if found in the file with open(filename, 'w') as f: s = f.read() print('Changing "{old_string}" to "{new_string}" in {filename}'.format(**locals())) s = s.replace(old_string, new_string) f.write(s) 

It is worth noting that if the file names were different, we could do it more elegantly with a single with statement.

+70
Jul 09 '13 at 12:43 on
source share
  #!/usr/bin/python with open(FileName) as f: newText=f.read().replace('A', 'Orange') with open(FileName, "w") as f: f.write(newText) 
+34
Feb 01 '16 at 12:26
source share

Something like

 file = open('Stud.txt') contents = file.read() replaced_contents = contents.replace('A', 'Orange') <do stuff with the result> 
+9
Nov 08 '10 at
source share
 with open('Stud.txt','r') as f: newlines = [] for line in f.readlines(): newlines.append(line.replace('A', 'Orange')) with open('Stud.txt', 'w') as f: for line in newlines: f.write(line) 
+6
Nov 08 '10 at
source share

If you are using Linux and just want to replace the word " dog with" cat you can do the following:

text.txt:

 Hi, i am a dog and dog are awesome, i love dogs! dog dog dogs! 

Linux command:

 sed -i 's/dog/cat/g' test.txt 

Exit:

 Hi, i am a cat and cat are awesome, i love cats! cat cat cats! 

Original post: https://askubuntu.com/questions/20414/find-and-replace-text-within-a-file-using-commands

+6
Dec 13 '17 at 0:09
source share

The easiest way to do this is with regular expressions, assuming that you want to iterate over each line in the file (where "A" will be stored).

 import re input = file('C:\full_path\Stud.txt), 'r') #when you try and write to a file with write permissions, it clears the file and writes only #what you tell it to the file. So we have to save the file first. saved_input for eachLine in input: saved_input.append(eachLine) #now we change entries with 'A' to 'Orange' for i in range(0, len(old): search = re.sub('A', 'Orange', saved_input[i]) if search is not None: saved_input[i] = search #now we open the file in write mode (clearing it) and writing saved_input back to it input = file('C:\full_path\Stud.txt), 'w') for each in saved_input: input.write(each) 
0
Nov 08 '10 at 21:48
source share



All Articles