Reputation: 538
(Using Python 3.4) So I have a big folder with 1500 folders in it. I want to analyze data that is 2 layers inside each folder. Each folder's name has the format:
YYMMDD_XXXXX_####X
where X
is a letter and #
is a number. All I really care about is the ####X
part which is unique. Inside this folder is a folder called "infosite-####X"
(the same ####X
as the parent folder), and inside this folder is an rst
file called "YYMMDD_XXXXX_####X_infosite.rst"
(also same ID) which has the data I want.
I've built the code for data analysis but I don't want to type out the whole directory when each directory has that unique 5mer sequence.
Right now I have the directory with a single file I want hard coded and do it like this:
import os
os.chdir("Z:/RunLog/160428_HV2VY_1892L/infosite-1892L")
user_input = input("Enter name of file: ")
assert os.path.exists(user_input + ".rst"), "I did not find the file at, " + str(user_input)
f = open(user_input + ".rst" ,'r+')
print("Hooray we found your file!")
ideally I'd like to have something like
os.chdir("Z:/RunLog/*" + user_input + "/*" user_input + "/*" user_input + ".rst")
but this isn't really allowed, and I don't see how to use glob.glob
with it.
Upvotes: 0
Views: 1028
Reputation: 168616
Just pass the expression you've created to glob()
, and use the result, like so:
import glob
import os
user_input = input("ID of file: ")
for filename in glob.glob(
"Z:/RunLog/*" + user_input + "/*" user_input + "/*" user_input + "_infosite.rst"):
f = open(filename)
...
Or, if you are not prepared to deal with multiple matching files:
user_input = input("ID of file: ")
filenames = glob.glob(
"Z:/RunLog/*" + user_input + "/*" user_input + "/*" user_input + "_infosite.rst")
filename = filenames[0]
f = open(filename)
Also note that the creation of the glob pattern might be more readable like so:
pattern = 'Z:/RunLog/*{0}/*{0}/*{0}_infosite.rst'.format(user_input)
filenames = glob.glob(pattern)
Upvotes: 1