检查给定目录中是否存在来自多个文件列表的文件

use*_*443 2 python

我需要检查多个文件列表并确定存在哪些文件。我已经通过以下方式尝试过这个,虽然我认为它可以做得更好。我在下面写了一些伪代码:

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_
Run Code Online (Sandbox Code Playgroud)

Vid*_*dul 6

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']
Run Code Online (Sandbox Code Playgroud)