【问题标题】:How to extract files from a particular folder with filename stored in a python list?如何从文件名存储在 python 列表中的特定文件夹中提取文件?
【发布时间】:2021-02-28 10:33:18
【问题描述】:

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

  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 file directory


    【解决方案1】:

    您可以使用以下 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 2013-11-06
      • 2020-10-31
      相关资源
      最近更新 更多