#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0

    Lightbulb Read Multiple text File From a Directory Usin Python


    Hi! all,

    I have 100 text file


    1. I need to Read all the Text File using a for loop

    so that i can display the content of all the file one after
    the other.

    2. I also want to read text file based on pattern of the filename prefix

    like.

    i have files like

    ITEM1.TXT
    ITEM2.TXT
    ITEM3.TXT

    DEMO1.TXT
    DEMO2.TXT
    DEMO3.TXT

    1. QUESTION IS TO READ ALL THE TEXT FILE

    2. READ ONLY TEXT FILE WITH PREFIX : DEMO

    DEMO1.TXT
    DEMO2.TXT
    DEMO3.TXT

    pLEASE HELP ME WITH THIS.

    tHANZ.

    sHAH
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    glob is your friend
    Code:
    import glob
    # all files
    for fil in glob.glob('*'):
       # do something with the files
    
    # DEMO files only
    for fil in glob.glob('DEMO*'):
        # do blabla
    Regards, mawe

    Comments on this post

    • Yegg` agrees
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You could also use the os modules listdir() function however you will need to check the prefix manually, you can do this using the startswith() string method.

    In both cases you should check that the item is a file unless you can be sure that no sub directories will ever be present since this will crash your program.

    Code:
    #!/usr/bin/env python
    
    import os
    
    path = ''
    
    for itemName in os.listdir(path):
        #Loops over each itemName in the path. Joins the path and the itemName
        #and assigns the value to itemName.
        itemName = os.path.join(path, itemName)
    
        if os.path.isfile(itemName):
            print file(itemName, 'r').read()
    In this example you can see the use of the os.path.isfile() function to check if the item is a file or not. You can also see the use of the os.path.join() function, which is used to join the path and the itemName in a way that is cross platform.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    Originally Posted by netytan
    You could also use the os modules listdir() function however you will need to check the prefix manually, you can do this using the startswith() string method.

    In both cases you should check that the item is a file unless you can be sure that no sub directories will ever be present since this will crash your program.

    Code:
    #!/usr/bin/env python
    
    import os
    
    path = ''
    
    for itemName in os.listdir(path):
        #Loops over each itemName in the path. Joins the path and the itemName
        #and assigns the value to itemName.
        itemName = os.path.join(path, itemName)
    
        if os.path.isfile(itemName):
            print file(itemName, 'r').read()
    In this example you can see the use of the os.path.isfile() function to check if the item is a file or not. You can also see the use of the os.path.join() function, which is used to join the path and the itemName in a way that is cross platform.

    Hope this helps,

    Mark.
    Thanz

    It worked

    Shah

IMN logo majestic logo threadwatch logo seochat tools logo