Aubrey
Aubrey

Reputation: 91

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)

Upvotes: 1

Views: 2285

Answers (2)

blueteeth
blueteeth

Reputation: 3555

Rather than looping through the files in the source directory it would be quicker to loop through the filenames you already have. You can use os.path.exists() to check if a file is available to be moved.

from os import path
import shutil

for filename in conden_art:
    src_fp, dst_fp = path.join(src, filename), path.join(dst, filename)
    if path.exists(filepath):
        shutil.move(src_fp, dst_fp)
        print(f'{src_fp} moved to {dst}')
    else:
        print(f'{src_fp} does not exist')

Upvotes: 0

Gary
Gary

Reputation: 899

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.

Upvotes: 1

Related Questions