[File Input Module]Replacing string in a file

V

vanam

Hi all,

For replacing string in the file, i have used the module File
input for replacing the string in the file.

For understanding and execution purpose, i have just included Python
as a string in the file and want it to be replaced to PYTHON.

Below are my queries and code: (Correct me if my understanding is
wrong???????)

1))

import fileinput
x = fileinput.input('data.txt',inplace=0)
for line in x:
line = line.replace('Python','PYTHON)
print line,
x.close()

The above piece of code will not create any backup file but it will
replace PYTHON (Print on the console) but not physically write to the
file.

2)))

import fileinput
x = fileinput.input('data.txt',inplace=1)
for line in x:
line = line.replace('Python','PYTHON')
print line,
x.close()

The above piece of code will create backup file but hidden (in the
form of bak file) and it will physically write to the file -- I have
verified the contents of data.txt after the file operation and it had
written successfully.But why it is not printing line i.e. string in
the file on the console.

3)))

import fileinput
x = fileinput.input('data.txt',inplace=1)
for line in x:
line = line.replace('Python','PYTHON')
x.close()

The above piece of code after execution is wiping out the full
contents. But addition of print line, is exactly replacing the string,
what exactly addition of print is making difference???

4)))

import fileinput
x = fileinput.input('data.txt',inplace=1,backup='content.txt')
for line in x:
line = line.replace('Python','PYTHON')
print line,
x.close()

The above piece is creating a backup file by name data.txtcontent.txt
(I am not sure whether created file name is correct or not?) and to
the back up file it had added previous content i.e., Python and it had
replaced the contents in data.txt to PYTHON
 
S

Steven D'Aprano

Hi all,

For replacing string in the file, i have used the module File input for
replacing the string in the file.

If you're processing a single file, using fileinput is overkill, and slow
as well. Here is a quick-and-dirty way of processing one file, with no
error checking:

text = open("some file", "r").read()
# modify the text however you like
text = text.replace("ham", "spam")
# replace in place
open("some file", "w").write(text)


In a real application, you would need to do significant error checking.
But it shows the basic technique: you read a file, modify it in memory,
then write it back to disk.

For understanding and execution purpose, i have just included Python as
a string in the file and want it to be replaced to PYTHON.

Below are my queries and code: (Correct me if my understanding is
wrong???????)

1))

import fileinput
x = fileinput.input('data.txt',inplace=0)
for line in x:
line = line.replace('Python','PYTHON)
print line,
x.close()

The above piece of code will not create any backup file but it will
replace PYTHON (Print on the console) but not physically write to the
file.
Correct.


2)))

import fileinput
x = fileinput.input('data.txt',inplace=1)
for line in x:
line = line.replace('Python','PYTHON')
print line,
x.close()

The above piece of code will create backup file but hidden (in the form
of bak file) and it will physically write to the file -- I have verified
the contents of data.txt after the file operation and it had written
successfully.But why it is not printing line i.e. string in the file on
the console.

You should read the documentation to fileinput.

The inplace=1 argument tells fileinput to redirect stdout to the current
file, so the output of print will go to the file instead of the console.

Personally, I find that an incredibly unintuitive and dangerous API, and
would recommend against fileinput just for that reason alone.


3)))

import fileinput
x = fileinput.input('data.txt',inplace=1)
for line in x:
line = line.replace('Python','PYTHON')
x.close()

The above piece of code after execution is wiping out the full contents.
But addition of print line, is exactly replacing the string, what
exactly addition of print is making difference???

That's how the module works: printing will go to the file instead of the
console. Since you don't print anything, the file gets written with the
empty string.

4)))

import fileinput
x = fileinput.input('data.txt',inplace=1,backup='content.txt')
for line in x:
line = line.replace('Python','PYTHON') print line,
x.close()

The above piece is creating a backup file by name data.txtcontent.txt

Yes, that's write, you told it to use "content.txt" as the file extension
on the backup file. So it starts with the existing name, "data.txt" and
adds "content.txt" as the extension, giving "data.txtcontent.txt".

You really should read the documentation. In the interactive interpreter:


import fileinput
help(fileinput)

(I am not sure whether created file name is correct or not?) and to the
back up file it had added previous content i.e., Python and it had
replaced the contents in data.txt to PYTHON

Is there a question there?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top