Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if files from multiple filelists exist in given directory

Tags:

python

I need to check multiple file lists and determine which files are present. I have tried this in the following way, although I think it can be done better. I wrote some pseudo-code below:

a_files = ["A", "B", "c"]
A_files = ["abc", "def", "fgh"]
a_file_found = None
A_file_found = None
for a_ in a_files:
  if os.path.isfile(a_):
      a_file_found = "B"
for A_ in A_files:
   if os.path.isfile(A_):
      A_file_found = a_
like image 551
user765443 Avatar asked Jan 30 '26 12:01

user765443


1 Answers

import os.path

# files "a" and "b" exist, "c" does not exist

a_files = ["a", "b", "c"];
a_exist = [f for f in a_files if os.path.isfile(f)];
a_non_exist = list(set(a_exist) ^ set(a_files))

print("existing: %s" % a_exist)            # ['a', 'b']
print("non existing: %s" % a_non_exist)    # ['c']
like image 153
Vidul Avatar answered Feb 01 '26 01:02

Vidul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!