How to read all files in a directory

Discussion in 'Python' started by hungbichvo, Nov 3, 2005.

  1. hungbichvo

    hungbichvo Guest

    Dear All,

    My python application is small. It reads data from a file.
    My code is:
    fileName = '900128.DAT'
    dataFile = open(fileName, 'r').readlines()
    I have to run 100 input files .DAT. Each time I run application, I have
    to change code fileName to a new one. For example, fileName
    = 'NewFile.DAT'.
    I do not know how I can process all file with extension .DAT in a
    specific directory once only.

    Any suggestion will be appreciated,

    Thank you.
     
    hungbichvo, Nov 3, 2005
    #1
    1. Advertising

  2. "hungbichvo" <> writes:

    > Dear All,
    >
    > My python application is small. It reads data from a file.
    > My code is:
    > fileName = '900128.DAT'
    > dataFile = open(fileName, 'r').readlines()
    > I have to run 100 input files .DAT. Each time I run application, I have
    > to change code fileName to a new one. For example, fileName
    > = 'NewFile.DAT'.
    > I do not know how I can process all file with extension .DAT in a
    > specific directory once only.
    >
    > Any suggestion will be appreciated,
    >
    > Thank you.


    http://www.python.org/doc/2.4.2/lib/module-glob.html

    % python
    Python 2.4.2 (#2, Sep 30 2005, 21:19:01)
    [GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu8)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import glob
    >>> print glob.glob('*.rb')

    ['hello.rb', 'test.rb', 'ping.rb', 'echo.rb']

    Sorry for the ruby code in the example :)

    S.
     
    Stefan Arentz, Nov 3, 2005
    #2
    1. Advertising

  3. hungbichvo

    Larry Bates Guest

    Not tested:

    import glob
    import os
    path=r'C:\datafiles\'
    for fileName in glob.glob(os.path.join(path,'*.DAT')):
    dataFile=open(fileName, 'r').readlines()
    .
    . Continue yur code here
    .



    -Larry Bates
    hungbichvo wrote:
    > Dear All,
    >
    > My python application is small. It reads data from a file.
    > My code is:
    > fileName = '900128.DAT'
    > dataFile = open(fileName, 'r').readlines()
    > I have to run 100 input files .DAT. Each time I run application, I have
    > to change code fileName to a new one. For example, fileName
    > = 'NewFile.DAT'.
    > I do not know how I can process all file with extension .DAT in a
    > specific directory once only.
    >
    > Any suggestion will be appreciated,
    >
    > Thank you.
    >
    >
    >
     
    Larry Bates, Nov 3, 2005
    #3
  4. hungbichvo

    Larry Bates Guest

    Not tested:

    import glob
    import os
    path=r'C:\datafiles\'
    for fileName in glob.glob(os.path.join(path,'*.DAT')):
    dataFile=open(fileName, 'r').readlines()
    .
    . Continue yur code here
    .



    -Larry Bates
    hungbichvo wrote:
    > Dear All,
    >
    > My python application is small. It reads data from a file.
    > My code is:
    > fileName = '900128.DAT'
    > dataFile = open(fileName, 'r').readlines()
    > I have to run 100 input files .DAT. Each time I run application, I have
    > to change code fileName to a new one. For example, fileName
    > = 'NewFile.DAT'.
    > I do not know how I can process all file with extension .DAT in a
    > specific directory once only.
    >
    > Any suggestion will be appreciated,
    >
    > Thank you.
    >
    >
    >
     
    Larry Bates, Nov 3, 2005
    #4
    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. TJS
    Replies:
    1
    Views:
    549
    Eliyahu Goldin
    Jun 23, 2004
  2. Replies:
    1
    Views:
    611
    Evertjan.
    Sep 30, 2005
  3. Replies:
    4
    Views:
    1,003
    M.E.Farmer
    Feb 13, 2005
  4. vsk
    Replies:
    5
    Views:
    875
  5. Replies:
    1
    Views:
    196
    Evertjan.
    Sep 30, 2005
Loading...

Share This Page