Shariful Islam
Shariful Islam

Reputation: 17

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

I have nearly 10k excel files in a folder and I have a python list of filenames of nearly 8k. I want to extract the files from that folder that are present in the list, and then keep that files in another folder. I have looked at these answers but none explains my issue:

  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

Any suggestions on how I can do that?

Upvotes: 0

Views: 870

Answers (1)

Suhas Kashyap
Suhas Kashyap

Reputation: 438

You can use this following python script, assuming you are using Linux or mac or if you are using windows us modify the mv command.

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)

Upvotes: 1

Related Questions