How to use os.path.isfile() on a list of paths

Question:

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?

Asked By: tharndt

||

Answers:

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()))
Answered By: Dan Lowe

You can iterate the line of your file and check if each path exist as follow:

import os

with open('list.txt', 'r') as fd:
    for line in fd:
        path = line.strip()  # drop n
        if os.path.isfile(path):
            print(path)
Answered By: Laurent LAPORTE

Consider

os.path.isfile("/tmp") # True

while

os.path.isfile("/tmpn") # False

try instead:

with open("/pathlist", "r") as f:
   for path in map(str.strip, f.readlines()):
       print( os.path.isfile(path))
Answered By: gbtimmon
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.