Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [File Input Module]Replacing string in a file

Reply
Thread Tools

[File Input Module]Replacing string in a file

 
 
vanam
Guest
Posts: n/a
 
      01-29-2010
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='conte nt.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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-29-2010
On Thu, 28 Jan 2010 21:36:27 -0800, vanam wrote:

> 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='conte nt.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?



--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
INPUT Type="FILE" - Just want to get the file name and path as string kevin@webpageconcepts.com ASP .Net 4 08-02-2006 05:47 PM
how to restrict user from input in <input type="file"> Tarkeshwar Javascript 2 04-17-2006 11:44 AM
using std::string; string("hello") vs std::string("hello") in header file. Fei Liu C++ 9 04-01-2006 08:49 AM
Getting User Input after getting Input from a file dei3cmix@uga.edu C++ 3 03-23-2006 05:01 AM
Input string was not in a correct format. on string "0" chongo ASP .Net 2 07-08-2004 02:48 AM



Advertisments