Reading and Writing

Discussion in 'Python' started by elias.goodman@gmail.com, Dec 23, 2004.

  1. Guest

    I am new to Python and programming. I am looking for some basic
    instruction here.

    How should I: Open a Text file, read from it, modify it, print to
    another .txt?

    For instance: Read a string, sort it, write the sorted string.

    I understand the algorithms but I don't know the actual mechanics of
    Python.

    Any help or advice as to where to look would be great.

    Thanks.
    , Dec 23, 2004
    #1
    1. Advertising

  2. Hi,

    wrote:
    > How should I: Open a Text file, read from it, modify it, print to
    > another .txt?
    >
    > For instance: Read a string, sort it, write the sorted string.


    What do you mean by "sorting"? If you want to sort the lines contained
    in a file, you could do something like this.

    $ cat in.txt
    foo
    bar
    baz
    ham
    spam
    $ cat process.py
    #!/usr/bin/env python
    lines = open("in.txt").readlines()
    lines.sort()
    out = open("out.txt", "w")
    for line in lines:
    out.write(line)
    out.close()
    $ python process.py
    $ cat out.txt
    bar
    baz
    foo
    ham
    spam

    Regards
    G.
    Sir Galahad the chaste, Dec 23, 2004
    #2
    1. Advertising

  3. Novitiate Guest

    -Galahad,

    Thank you very much. I will give it a shot and see if I can make it
    hapen. I think this will help a lot.

    I was just trying to implement a simple sorting algorithm that I knew
    from C++, for practice but I couldn't figure the mechanics of Python.
    Thanks again,

    Novitiate
    Novitiate, Dec 23, 2004
    #3
  4. Novitiate Guest

    -Galahad,

    Thank you very much. I will give it a shot and see if I can make it
    hapen. I think this will help a lot.

    I was just trying to implement a simple sorting algorithm that I knew
    from C++, for practice but I couldn't figure the mechanics of Python.
    Thanks again,

    Novitiate
    Novitiate, Dec 23, 2004
    #4
  5. Mike Meyer Guest

    Sir Galahad the chaste <> writes:

    > Hi,
    >
    > wrote:
    >> How should I: Open a Text file, read from it, modify it, print to
    >> another .txt?
    >> For instance: Read a string, sort it, write the sorted string.

    >
    > What do you mean by "sorting"? If you want to sort the lines contained
    > in a file, you could do something like this.
    >
    > $ cat in.txt
    > foo
    > bar
    > baz
    > ham
    > spam
    > $ cat process.py
    > #!/usr/bin/env python
    > lines = open("in.txt").readlines()


    Some people consider it bad style to leave opened files lieing around,
    so this shold be:

    f = open("in.txt")
    lines = f.readlines()
    f.close()

    > lines.sort()
    > out = open("out.txt", "w")
    > for line in lines:
    > out.write(line)


    Those two should be
    out.writelines(lines)

    > out.close()


    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Dec 24, 2004
    #5
    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. Pieter Hulshoff
    Replies:
    0
    Views:
    1,049
    Pieter Hulshoff
    Aug 13, 2003
  2. Chris Pruett
    Replies:
    0
    Views:
    1,412
    Chris Pruett
    Aug 14, 2003
  3. Mark Stijnman
    Replies:
    4
    Views:
    331
    Mark Stijnman
    May 12, 2005
  4. Replies:
    7
    Views:
    524
  5. Replies:
    3
    Views:
    175
    Bob Barrows [MVP]
    Jan 25, 2006
Loading...

Share This Page