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

    Join Date
    Jan 2005
    Posts
    7
    Rep Power
    0

    Red face Search for files (python)


    Hi all

    Can anyone please help me with some source code to search for .txt files in current dir and save the result to a variable like 'txtFound' if you dont understand ill give you a batch example(Hope ya know batch )

    FOR %%B IN (*.txt) DO SET txtFound=%%B
    Echo %txtFound%
    Pause

    Ok thats a nice example

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

    Join Date
    Jan 2005
    Posts
    37
    Rep Power
    10
    I dont know batch, but this works...

    >>> import glob
    >>> glob.glob('*.txt')
    ['dummy.txt', 'message.txt', 'optLog.txt', 'README.txt', 'result.txt', 'X_2_trn.txt', 'X_2_tst.txt', 'X_2_val.txt', 'Y_2_trn.txt', 'Y_2_val.txt']

    It returns a list of all the files in the current directory matching the pattern specified. You can enter a path as well..
    Hope this helps.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    7
    Rep Power
    0
    thnx
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    40
    Rep Power
    10
    Originally Posted by Zone
    I dont know batch, but this works...

    >>> import glob
    >>> glob.glob('*.txt')
    ['dummy.txt', 'message.txt', 'optLog.txt', 'README.txt', 'result.txt', 'X_2_trn.txt', 'X_2_tst.txt', 'X_2_val.txt', 'Y_2_trn.txt', 'Y_2_val.txt']

    It returns a list of all the files in the current directory matching the pattern specified. You can enter a path as well..
    Hope this helps.

    this code can runs in Windows ??
    as i know, "glob" is only works in UNIX. can anyone confirm with me about it?
  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    The glob module works on Unix and Windows. The module docs will tell you about any restrictions.

    grim

IMN logo majestic logo threadwatch logo seochat tools logo