1

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".

  • Any idea how to do this in Python?

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)
Aubrey
  • 91
  • 1
  • 7
  • I have an idea. What have you tried? – blueteeth Jan 27 '20 at 15:30
  • Please edit your post to explain what you've tried and where you're getting stuck. – bruno desthuilliers Jan 27 '20 at 15:31
  • Here is a hint for you - look into [listdir](https://www.tutorialspoint.com/python/os_listdir.htm) and [shutil.move](https://docs.python.org/2/library/shutil.html) – Gary Jan 27 '20 at 15:33
  • Does this answer your question? [How to move a file in Python](https://stackoverflow.com/questions/8858008/how-to-move-a-file-in-python) – Bram Vanroy Jan 27 '20 at 15:41
  • Hi everyone, thanks for the feedback. I've added what I've tried to the post. In addition, I've outline how the problem is different than just moving files from one place to another. The part I'm having problems with is finding and moving the files that match files from a list. – Aubrey Jan 27 '20 at 15:50

2 Answers2

1

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.

Gary
  • 909
  • 8
  • 20
0

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')
blueteeth
  • 3,330
  • 1
  • 13
  • 23
  • Thanks for the suggestion! I got a slower code to work, but I'll try to incorporate your answer later. – Aubrey Jan 27 '20 at 17:06