[File Input Module]Replacing string in a file

Discussion in 'Python' started by vanam, Jan 29, 2010.

  1. vanam

    vanam Guest

    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
     
    vanam, Jan 29, 2010
    #1
    1. Advertising

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



    --
    Steven
     
    Steven D'Aprano, Jan 29, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher R. Barry

    Re: Searching/replacing multiple words in input.

    Christopher R. Barry, Oct 11, 2003, in forum: Java
    Replies:
    1
    Views:
    2,061
    John Kordyback
    Oct 12, 2003
  2. Tung Chau
    Replies:
    1
    Views:
    470
    SM Ryan
    Aug 6, 2004
  3. Tung Chau
    Replies:
    0
    Views:
    377
    Tung Chau
    Aug 6, 2004
  4. Sidhu

    Sentence input and replacing

    Sidhu, Jun 17, 2006, in forum: C Programming
    Replies:
    4
    Views:
    299
    Keith Thompson
    Jun 17, 2006
  5. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    282
    Chris Hohmann
    Apr 11, 2005
Loading...

Share This Page