December 24th, 2012, 06:57 PM
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.
December 24th, 2012, 09:46 PM
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.
December 24th, 2012, 10:14 PM
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()
December 25th, 2012, 12:03 PM
Thanks all. I got it to work. I really appreciate the help.
December 25th, 2012, 04:39 PM
Too late probably, but look at glob.glob in the future
files = glob.glob( "AML*" )