Reputation: 1801
I have a list of file paths listt
:
['/data0/river_routes.nc',
'/dat/rho0_profile.nc',...]
I want to check if they exist, and if not - create a list list1
with just the paths of the missing files.
I can create list1
full of True/False
using:
import os,time
list1 = []
for file in listt:
list1.append(os.path.isfile(file))
But I'm not sure how to create a list of the missing paths in the most efficient way
Upvotes: 0
Views: 118
Reputation: 27495
You need to append the path not the bool
that isfile
returns.
from os.path import isfile
paths = ['/data0/river_routes.nc', '/dat/rho0_profile.nc',...]
missing_paths = []
for path in paths:
if not isfile(path):
missing_paths.append(path)
This can also be done using a list comprehension:
missing_paths = [path for path in paths if not isfile(path)]
Or you can use itertools.filterfalse
:
from itertools import filterfalse as ffilter
missing_paths = list(filter(ispath, paths))
Upvotes: 2
Reputation: 364
you can use list comprehension
import os
not_exists = [f for f in list1 if not os.path.exists(f)]
where list1 is list with paths
Upvotes: 1
Reputation: 940
Just subtract a set of found from all:
import os
path = 'path/to/directory'
listt = ['/data0/river_routes.nc',
'/dat/rho0_profile.nc',...]
all = set(listt)
found = set(os.listdir(path))
missing = list(all - found)
Upvotes: 1
Reputation: 11091
Try this one:
from pathlib import Path
if __name__ == '__main__':
paths = ['/data0/river_routes.nc',
'/dat/rho0_profile.nc']
missing = []
for item in paths:
p = Path(item)
if not p.exists():
missing.append(item)
Upvotes: 2