I'm using linux8. I've got a repo (with subrepos) containing files and I have a list of the filenames ('path/to/file/filename.pdf'
). I want to check (using python), if those files all do exist and if not, I want to know that. So I tried reading the list, iterating the list entries with a for loop and using those with os.path.isfile()
.
E.g. I've got a repo containing following files: list.txt, test1.txt and test2.txt.
The list.txt contains the filenames (here: 'test1.txt' 'test2.txt').
os.path.isfile('test1.txt')
gives a
True
But this for loop...
import os
with open('list.txt', 'r') as f:
pathlist=f.readlines()
for path in pathlist:
print(os.path.isfile(path))
...gives:
False
False
although
type(path)
is
<type 'str'>
It's feels like python distinguishes two types of strings. Does anyone know, where that comes from?
Two possible problems.
First, you might not be running in the directory you believe you are.
Second, readlines()
will return the lines with newlines and possibly carriage returns attached. You'll want to remove those before testing them as paths. You can use rstrip()
to remove trailing whitespace from a string.
for path in pathlist:
print(os.path.isfile(path.rstrip()))