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

    Join Date
    Sep 2005
    Posts
    54
    Rep Power
    9

    Opening certain files within a directory


    I have a very basic question about creating a list of files. I would like to:

    1. open a directory specified by a path

    2. add all files that match a certain pattern onto a file list (let's say files that start with "AML_" followed by 6 capital letters followed by an underscore and then 6 numbers and ending in ".sam")

    3. close the directory

    Now I can go through each file in the list and process it appropriately. I do this a lot in perl, but I'm new to python and I'm having trouble figuring out this basic operation.

    Thank you.

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

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Look at the os module for functions that interact with the file system (in particular the function os.listdir is what you want on that end) and the re module for regular expressions.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0
    os.listdir() will list a directorys' content

    instead of re module using regular expression you could also parse out the unwated contents via the glob.glob()
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    54
    Rep Power
    9
    Thanks all. I got it to work. I really appreciate the help.

    Eric
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    451
    Rep Power
    32
    Too late probably, but look at glob.glob in the future
    Code:
    files = glob.glob( "AML*" )

IMN logo majestic logo threadwatch logo seochat tools logo