0

I'm trying to copy files from directory A, to directory B, based on a txt file containing the list of files to be extracted - located in directory B. I referred to this code: How to extract files from a particular folder with filename stored in a python list?

but it doesn't seem to enter the if (where I have put the 'in here' printout). Could someone tell me what I am doing wrong?

This is the code:

import os
import shutil

def read_input_file():
    my_file = open("/mnt/d/Downloads/TSU/remaining_files_noUSD_19Jan.txt", "r")
    # reading the file
    data = my_file.read()
    data_into_list = data.split("\n")
    #print(data_into_list)
    my_file.close()
    return data_into_list


def filter_data(list_of_files):
    path="/mnt/e/Toyota Smarthome/Untrimmed/Videos_mp4"
    path_to_be_moved="/mnt/d/Downloads/TSU"
    #print(list_of_files)
    for file in os.listdir(path):
        #print(file)
        if file in list_of_files:
            print("in here")
            print(file)
            shutil.copytree(path,path_to_be_moved)
            #os.system("mv "+path+file+" "+path_to_be_moved)
            
if __name__ == "__main__":
    list = read_input_file()
    filter_data(list)

I am using python3 via WSL.

the mp4 folder contains multiple videos, and the output of "

read input file
is as follows output for read input file

"

Thank you!

Megan Darcy
  • 530
  • 5
  • 15
  • Adding some more information to this question would help. Can you show an example of the contents of `/mnt/e/Toyota Smarthome/Untrimmed/Videos_mp4`? Can you print out the value returned by `read_input_file`? – larsks Jan 20 '23 at 14:53
  • Also, unrelated to your question, never name a Python variable `list`; this masks the built-in `list` type and will ultimately cause problems if you need to call `list(something)`. – larsks Jan 20 '23 at 14:55
  • 2
    Just to clarify - are you looking to COPY the files, or MOVE the files? Also, your file list appears to be only the file stems, and doesn't include the file extensions, so as an example: `if "P12T05C05.mp4" in list_of_files:` will be False, because the list has only "P12T05C05". – nigh_anxiety Jan 20 '23 at 15:33
  • 1
    @nigh_anxiety thank youuuu so much!! this solved it :D yup trying to copy the file! – Megan Darcy Jan 20 '23 at 15:35

1 Answers1

0

I think copytree from shutil has another purpose to just move file, it moves an entire structure. I'd use shutil.move

import os
import shutil

def read_input_file():
    my_file = open("list.txt", "r")
    data = my_file.read()
    data_into_list = data.split("\n")
    my_file.close()
    return data_into_list


def filter_data(list_of_files):
    path="directoryA/"
    path_to_be_moved="directoryB/"
    for file in os.listdir(path):
        # print(file)
        if file in list_of_files:
            print("in here")
            print(file)
            shutil.move(path+file,path_to_be_moved+file)


mylist = read_input_file()
filter_data(mylist)

just saw your update, be careful, data_into_list = data.split("\n") is for a file.txt with a list separated with an enter. yours is with a comma and space so you'll have to change that.

Also you shouldn't use list as a variable name, mylist for example is better. list() is used to create list

adriencdw
  • 21
  • 3