Reputation: 47
The contents of the file myfilepaths
is:
/current/0.e_head/10000000d.000000002__head
/current/0.13_head/10000008.000000001__head
..(cont'd)
The actual filenames are:
/current/0.e_head/10000000d.000000002__head_918293
/current/0.13_head/10000008.000000001__head_9891283
..(cont'd)
My code is:
filepath = '/path/to/directory/myfilepaths'
location = raw_input('Where is the file? ')
with open(filepath, 'r') as mf:
for line in mf:
nline = line.replace("\n", "")
# cline = glob.glob(nline + '*') #PROBLEM LINE
path_obj = os.path.join(location + str(nline))
obj_stats = os.stat(path_obj)
What I am looking to do is to employ glob to complete the filename for me, as the end of the filename is not deterministic enough for it to be hardcoded. The location
created via raw_input typically is something like /var/local/ etc.
At the moment, what if I use the code pasted here, I get the error message:
no such file or directory: '/var/local/0.e_head/1000000d.000000002_head'
If I delete the comment and replace nline
with cline
in path_obj
I get:
OSError: [Errno 2] No such file or directory '/var/local[]'
Now I've seen a lot on SO which shows how to use glob to match certain files such as *.txt and so on, but I haven't seen anything which uses glob to find the end of search criteria
Upvotes: 0
Views: 321
Reputation: 180411
glob returns a list so calling str on it is not going to work even if you found a match, you need to also join the file name to the path when you are searching:
from glob import iglob
with open(filepath, 'r') as mf:
for line in mf:
path_obj = next(iglob(os.path.join(location, line.strip() + '*')))
obj_stats = os.stat(path_obj)
Once the path is correct glob should find the file just fine, the only issue is if you had overlapping patterns, the first part of each file name must be unique for your logic to work. I also presume '/path/to/directory/myfilepaths'
is a file that lists the partial file names.
Upvotes: 1