[Tutor] [File Input Module]Replacing string in a file

Dave Angel davea at ieee.org
Thu Jan 28 21:25:46 CET 2010


vanam wrote:
> Hi all,
>
> As it was suggested before in the mailing list about the query
> regarding 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.
>
>   
When you use the inplace=true option, it will redirect standard output 
to the file.  So print is going there, and *not* to the console.  I 
don't know whether close() restores the original console or not.
> 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???
>
>   
See above.  Since you print nothing to sys.stdout, the output file is empty.
> 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
>
> 5)))
>
> Suppose if data.txt has string Python written in Font size 72 and when
> i display the string on the console ie. by below piece of code
>
> import fileinput
> x = fileinput.input('data.txt',inplace=0)
> for line in x:
>       print line,
> x.close()
>
> It wouldnt print with the same Font size on the console (This wont
> prove anything wrong as the same font could be backed with a different
> file name)
>
>   
Text files have no concept of fonts or color.  Sometimes there are extra 
annotations in a file (eg. escape sequences) which can be interpreted by 
particular software as commands to change font, or change color, or even 
to reposition.  Examples of this would be html, postscript, rich-text, 
and ANSI escape sequences.

But those escape sequences will only be meaningful to a program that 
understands them.  So if you print html files out to the console, you'll 
see lots of angle brackets and such, rather than seeing the pretty 
display intended to show in a browser.  If you print to a console, it's 
up to that console to process some escape sequences (eg. ANSI) or not.
> Do let me know if my understanding is correct.
>   


More information about the Tutor mailing list