如何从文件名存储在 python 列表中的特定文件夹中提取文件?

How to extract files from a particular folder with filename stored in a python list?

我在一个文件夹中有将近 10k excel 个文件,并且我有一个 python 近 8k 的文件名列表。我想从列表中存在的该文件夹中提取文件,然后将这些文件保存在另一个文件夹中。我查看了这些答案,但 none 解释了我的问题:

  1. Get a filtered list of files in a directory
  2. Python: List all the file names in a directory and its subdirectories and then print the results in a txt file
  3. Use a list of values to select rows from a pandas dataframe

有什么关于如何做到这一点的建议吗?

您可以使用以下 python 脚本,假设您使用的是 Linux 或 mac,或者如果您使用的是 windows 我们修改 mv 命令。

import os
def filter_data(list_of_files):
    path="/absolute_path_to_directory_to_be_searched"
    path_to_be_moved="/ablosute_path_to_file_to_be_moved"
    for file in os.listdir(path):
        if file in list_of_files:
            os.system("mv "+path+file+" "+path_to_be_moved)
            
if __name__ == "__main__":
    filter_data(list)