Reading from text

Discussion in 'Python' started by oamram, Feb 17, 2009.

  1. oamram

    oamram Guest

    Hi All,
    new to python. i have a directory with about 50 text file and i need to
    iterate through them and get
    line 7 to 11 from each file and write those lines into another file(one file
    that will contain all lines).

    Cheers, Omer.
    --
    View this message in context: http://www.nabble.com/Reading-from-text-tp22061427p22061427.html
    Sent from the Python - python-list mailing list archive at Nabble.com.
    oamram, Feb 17, 2009
    #1
    1. Advertising

  2. oamram

    JB Guest

    oamram a écrit :
    > Hi All,
    > new to python. i have a directory with about 50 text file and i need to
    > iterate through them and get
    > line 7 to 11 from each file and write those lines into another file(one file
    > that will contain all lines).


    First create a function that read and parse one file

    Then create a loop that call this function for each file in a directory

    Modules to read :

    http://www.python.org/doc/2.5.2/tut/node9.html#SECTION009210000000000000000
    http://docs.python.org/library/os.html


    Julien
    JB, Feb 17, 2009
    #2
    1. Advertising

  3. oamram

    Guest

    oamram:
    > i have a directory with about 50 text file and i need to
    > iterate through them and get
    > line 7 to 11 from each file and write those lines into another
    > file(one file that will contain all lines).


    Files can be iterated line-by-line, so this idiom:

    for line in file: ...

    will give you the lines, with newline. Once you have debugged that,
    you can use the standard module glob (http://docs.python.org/library/
    glob.html#module-glob ) to iterate on files.

    If you have more problems, show use the code you have written and we
    may suggest improvements.

    Bye,
    bearophile
    , Feb 17, 2009
    #3
  4. oamram

    MRAB Guest

    wrote:
    > oamram:
    >> i have a directory with about 50 text file and i need to
    >> iterate through them and get
    >> line 7 to 11 from each file and write those lines into another
    >> file(one file that will contain all lines).

    >
    > Files can be iterated line-by-line, so this idiom:
    >
    > for line in file: ...
    >
    > will give you the lines, with newline. Once you have debugged that,
    > you can use the standard module glob (http://docs.python.org/library/
    > glob.html#module-glob ) to iterate on files.
    >

    [snip]
    Or:

    for index, line in enumerate(my_file): ...

    where index will give you the line number (starting from 0, so you'll
    want lines 6 to 10). You can break out of the loop when you have all the
    lines you want.

    > If you have more problems, show use the code you have written and we
    > may suggest improvements.
    >
    MRAB, Feb 17, 2009
    #4
  5. oamram

    Tim Chase Guest

    > Assuming this is a real task and not a homework problem, then
    > I'd do it this way:
    >
    > $ cd [directory containing 50 test files]
    > $ (for file in *; do head -n11 $file | tail -n5; done) >/path/to/results-file.txt


    I'd use sed:

    sed -ns 7,11p /source/path/*.txt >/path/to/results.txt

    hard to get much more concise than that with any common tool :)

    -tkc
    Tim Chase, Feb 17, 2009
    #5
  6. oamram

    Aahz Guest

    In article <>,
    Tim Chase <> wrote:
    >> Assuming this is a real task and not a homework problem, then
    >> I'd do it this way:
    >>
    >> $ cd [directory containing 50 test files]
    >> $ (for file in *; do head -n11 $file | tail -n5; done) >/path/to/results-file.txt

    >
    >I'd use sed:
    >
    > sed -ns 7,11p /source/path/*.txt >/path/to/results.txt
    >
    >hard to get much more concise than that with any common tool :)


    But you do have to learn sed. Avoiding that is precisely why I stick
    with Python.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "All problems in computer science can be solved by another level of
    indirection." --Butler Lampson
    Aahz, Mar 6, 2009
    #6
  7. oamram

    Paul McGuire Guest

    On Feb 17, 11:03 am, oamram <> wrote:
    > Hi All,
    > new to python. i have a directory with about 50 text file and i need to
    > iterate through them and get
    > line 7 to 11 from each file and write those lines into another file(one file
    > that will contain all lines).
    >


    import glob
    file("output.txt","w").write('\n'.join( "".join(f.readlines()
    [7:11+1]) ) for f in glob.glob("targetdir/*.txt"))

    -- Paul
    Paul McGuire, Mar 6, 2009
    #7
  8. oamram

    Steve Holden Guest

    Aahz wrote:
    > In article <>,
    > Tim Chase <> wrote:
    >>> Assuming this is a real task and not a homework problem, then
    >>> I'd do it this way:
    >>>
    >>> $ cd [directory containing 50 test files]
    >>> $ (for file in *; do head -n11 $file | tail -n5; done) >/path/to/results-file.txt

    >> I'd use sed:
    >>
    >> sed -ns 7,11p /source/path/*.txt >/path/to/results.txt
    >>
    >> hard to get much more concise than that with any common tool :)

    >
    > But you do have to learn sed. Avoiding that is precisely why I stick
    > with Python.


    +1 [cross-platform] QOTW
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Want to know? Come to PyCon - soon! http://us.pycon.org/
    Steve Holden, Mar 9, 2009
    #8
  9. oamram

    Steve Holden Guest

    Aahz wrote:
    > In article <>,
    > Tim Chase <> wrote:
    >>> Assuming this is a real task and not a homework problem, then
    >>> I'd do it this way:
    >>>
    >>> $ cd [directory containing 50 test files]
    >>> $ (for file in *; do head -n11 $file | tail -n5; done) >/path/to/results-file.txt

    >> I'd use sed:
    >>
    >> sed -ns 7,11p /source/path/*.txt >/path/to/results.txt
    >>
    >> hard to get much more concise than that with any common tool :)

    >
    > But you do have to learn sed. Avoiding that is precisely why I stick
    > with Python.


    +1 [cross-platform] QOTW
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Want to know? Come to PyCon - soon! http://us.pycon.org/
    Steve Holden, Mar 9, 2009
    #9
    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. Darrel
    Replies:
    3
    Views:
    657
    Kevin Spencer
    Nov 11, 2004
  2. Wael Soliman

    ASP.NET Reading problem (reading .xls)

    Wael Soliman, Jan 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,780
    =?Utf-8?B?dmluYXk=?=
    Jan 3, 2005
  3. Replies:
    0
    Views:
    771
  4. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    356
    Hrvoje Niksic
    May 25, 2007
  5. leo
    Replies:
    1
    Views:
    261
    Bob Lehmann
    Dec 5, 2005
Loading...

Share This Page