python-3.xmatchfile-move

How to match a list to file names, then move matched files to new directory in Python?


I have a folder of 90,000 PDF documents with sequential numeric titles (e.g. 02.100294.PDF). I have a list of around 70,000 article titles drawn from this folder. I want to build a Python program that matches titles from the list to titles in the folder and then moves the matched files to a new folder.

For example, say I have the following files in "FOLDER";

1.100.PDF
1.200.PDF
1.300.PDF
1.400.PDF

Then, I have a list with of the following titles

1.200.PDF
1.400.PDF

I want a program that matches the two document titles from the list (1.200 and 1.400) to the documents in FOLDER, and then move these two files to "NEW_FOLDER".

Thank you!

EDIT: This is the code I currently have. The source directory is 'scr', and 'dst' is the new destination. "Conden_art" is the list of files I want to move. I am trying to see if the file in 'scr' matches a name listed in 'conden_art'. If it does, I want to move it to 'dst'. Right now, the code is finding no matches and is only printing 'done'. This issue is different from just moving files because I need to match file names to a list, and then move them.

import shutil
import os

for file in scr:
    if filename in conden_art:
        shutil.copy(scr, dst)
    else:
        print('done')

SOLVED!

Here is the code I used that ended up working. Thanks for all of your help!

import shutil
import os
import pandas as pd

scr = filepath-1
dst = filepath-2

files = os.listdir(scr)

for f in files:
    if f in conden_art:
        shutil.move(scr + '\\' + f, dst)

Solution

  • Here's a way to do it -

    from os import listdir
    from os.path import isfile, join
    import shutil
    
    files = [f for f in listdir(src) if isfile(join(src, f))] # this is your list of files at the source path
    
    for i in Conden_art:
        if i in files:
           shutil.move(i,dst+i)  # moving the files in conden_art to dst/
    

    src and dst here are your paths for source and destination. Make sure you are at the src path before running the for loop. Otherwise, python will be unable to find the file.