Need Script For read multiple files(.txt) from a folder

Discussion in 'Python' started by jai_python, Mar 14, 2008.

  1. jai_python

    jai_python Guest

    hi frenz I Need a Python Script For read multiple files(.txt) from a
    folder and write it in a single text file....


    Thanks
     
    jai_python, Mar 14, 2008
    #1
    1. Advertising

  2. jai_python

    Chris Guest

    On Mar 14, 6:28 am, jai_python <> wrote:
    > hi frenz I  Need a Python Script For read multiple files(.txt) from a
    > folder and write it in a single text file....
    >
    > Thanks


    Take a look at the OS Module for the listdir funtion, you can use it
    to build a list of all files in the given folder. Iterate through the
    list checking to see if the file is of the correct type and if it is
    then append/write it to your single file. Don't forget to flush()
    your output otherwise you can easily run into memory issues.
     
    Chris, Mar 14, 2008
    #2
    1. Advertising

  3. On Thu, 13 Mar 2008 21:28:18 -0700 (PDT), jai_python
    <> declaimed the following in comp.lang.python:

    > hi frenz I Need a Python Script For read multiple files(.txt) from a
    > folder and write it in a single text file....
    >

    If you are on windows, just open a command prompt and use the
    standard copy command...

    C:\Documents and Settings\Dennis Lee Bieber>copy /?
    Copies one or more files to another location.

    COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/A | /B ] source [/A | /B]
    [+ source [/A | /B] [+ ...]] [destination [/A | /B]]

    source Specifies the file or files to be copied.
    /A Indicates an ASCII text file.
    /B Indicates a binary file.
    /D Allow the destination file to be created decrypted
    destination Specifies the directory and/or filename for the new
    file(s).
    /V Verifies that new files are written correctly.
    /N Uses short filename, if available, when copying a file
    with a
    non-8dot3 name.
    /Y Suppresses prompting to confirm you want to overwrite an
    existing destination file.
    /-Y Causes prompting to confirm you want to overwrite an
    existing destination file.
    /Z Copies networked files in restartable mode.

    The switch /Y may be preset in the COPYCMD environment variable. This
    may be overridden with /-Y on the command line. Default is to prompt on
    overwrites unless COPY command is being executed from within a batch
    script.

    To append files, specify a single file for destination, but multiple
    files for source (using wildcards or file1+file2+file3 format).

    C:\Documents and Settings\Dennis Lee Bieber>

    Note that last paragraph "To append files"
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Mar 14, 2008
    #3
  4. jai_python

    Chris Guest

    On Mar 14, 8:36 am, Dennis Lee Bieber <> wrote:
    > On Thu, 13 Mar 2008 21:28:18 -0700 (PDT), jai_python
    > <> declaimed the following in comp.lang.python:
    >
    > > hi frenz I  Need a Python Script For read multiple files(.txt) from a
    > > folder and write it in a single text file....

    >
    >         If you are on windows, just open a command prompt and use the
    > standard copy command...
    >
    > C:\Documents and Settings\Dennis Lee Bieber>copy /?
    > Copies one or more files to another location.
    >
    > COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/A | /B ] source [/A | /B]
    >      [+ source [/A | /B] [+ ...]] [destination [/A | /B]]
    >
    >   source       Specifies the file or files to be copied.
    >   /A           Indicates an ASCII text file.
    >   /B           Indicates a binary file.
    >   /D           Allow the destination file to be created decrypted
    >   destination  Specifies the directory and/or filename for the new
    > file(s).
    >   /V           Verifies that new files are written correctly.
    >   /N           Uses short filename, if available, when copying a file
    > with a
    >                non-8dot3 name.
    >   /Y           Suppresses prompting to confirm you want to overwrite an
    >                existing destination file.
    >   /-Y          Causes prompting to confirm you want to overwrite an
    >                existing destination file.
    >   /Z           Copies networked files in restartable mode.
    >
    > The switch /Y may be preset in the COPYCMD environment variable. This
    > may be overridden with /-Y on the command line.  Default is to prompt on
    > overwrites unless COPY command is being executed from within a batch
    > script.
    >
    > To append files, specify a single file for destination, but multiple
    > files for source (using wildcards or file1+file2+file3 format).
    >
    > C:\Documents and Settings\Dennis Lee Bieber>
    >
    >         Note that last paragraph "To append files"
    > --
    >         Wulfraed        Dennis Lee Bieber               KD6MOG
    >                    
    >                 HTTP://wlfraed.home.netcom.com/
    >         (Bestiaria Support Staff:               )
    >                 HTTP://www.bestiaria.com/


    If you want to go that route you could also do: type *.txt >
    output_file.txt
     
    Chris, Mar 14, 2008
    #4
  5. jai_python

    Guest

    use the glob module

    import os, glob
    dor = the path you want
    for dir, subdir, files in os.walk(dor):
    for file in files:
    if glob.fnmatch.fnmatch(file,"*.txt"):
    do what you want
     
    , Mar 14, 2008
    #5
  6. jai_python

    Jeff Schwab Guest

    Chris wrote:
    > On Mar 14, 8:36 am, Dennis Lee Bieber <> wrote:
    >> On Thu, 13 Mar 2008 21:28:18 -0700 (PDT), jai_python
    >> <> declaimed the following in comp.lang.python:
    >>
    >>> hi frenz I Need a Python Script For read multiple files(.txt) from a
    >>> folder and write it in a single text file....

    >> If you are on windows, just open a command prompt and use the
    >> standard copy command...
    >>
    >> C:\Documents and Settings\Dennis Lee Bieber>copy /?

    ....
    > If you want to go that route you could also do: type *.txt >
    > output_file.txt


    On Unix, cygwin, etc:

    cat dir/*.txt > output.txt

    Or if you need "deep" copy:

    cat $(find dir -name '*.txt') > output.txt

    You could write a portable solution in Python (as in Martin Laloux's
    post), but most modern command-line environments have similar (but not
    identical) support for globbing and redirecting files. If you're
    getting the glob pattern from a user, they may expect subtly
    platform-dependent behaviors, in which case portability might not as
    important as native feel.
     
    Jeff Schwab, Mar 14, 2008
    #6
  7. jai_python

    jai_python Guest

    On Mar 14, 9:45 pm, Jeff Schwab <> wrote:
    > Chris wrote:
    > > On Mar 14, 8:36 am, Dennis Lee Bieber <> wrote:
    > >> On Thu, 13 Mar 2008 21:28:18 -0700 (PDT), jai_python
    > >> <> declaimed the following in comp.lang.python:

    >
    > >>> hi frenz I Need a Python Script For read multiple files(.txt) from a
    > >>> folder and write it in a single text file....
    > >> If you are on windows, just open a command prompt and use the
    > >> standard copy command...

    >
    > >> C:\Documents and Settings\Dennis Lee Bieber>copy /?

    > ...
    > > If you want to go that route you could also do: type *.txt >
    > > output_file.txt

    >
    > On Unix, cygwin, etc:
    >
    > cat dir/*.txt > output.txt
    >
    > Or if you need "deep" copy:
    >
    > cat $(find dir -name '*.txt') > output.txt
    >
    > You could write a portable solution in Python (as in Martin Laloux's
    > post), but most modern command-line environments have similar (but not
    > identical) support for globbing and redirecting files. If you're
    > getting the glob pattern from a user, they may expect subtly
    > platform-dependent behaviors, in which case portability might not as
    > important as native feel.


    ya its working.... thanks for all ur help
     
    jai_python, Mar 15, 2008
    #7
    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. Replies:
    4
    Views:
    957
    M.E.Farmer
    Feb 13, 2005
  2. Sameen
    Replies:
    2
    Views:
    446
    Victor Bazarov
    Aug 29, 2005
  3. zl2k
    Replies:
    2
    Views:
    316
    James Kanze
    Mar 30, 2007
  4. Replies:
    7
    Views:
    398
    Gabriel Genellina
    Sep 25, 2007
  5. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,578
    Roedy Green
    Sep 15, 2011
Loading...

Share This Page