-Python- Move All PDF Files in Folder to NewDirect

2019-07-26 01:30发布

I'm trying to write code that will move hundreds of PDF files from a :/Scans folder into another directory based on the matching each client's name. I'm not sure if I should be using Glob, or Shutil, or a combination of both. My working theory is that Glob should work for such a program, as the glob module "finds all the pathnames matching a specified pattern," and then use Shutil to physically move the files?

Here is a breakdown of my file folders to give you a better idea of what I'm trying to do.

Within :/Scans folder I have thousands of PDF files, manually renamed based on client and content, such that the folder looks like this:

lastName, firstName - [contentVariable] (repeat the above 100,000x)

Within the :/J drive of my computer I have a folder named 'Clients' with sub-folders for each and every client, similar to the pattern above, named as 'lastName, firstName'

I'm looking to have the program go through the :/Scans folder and move each PDF to the matching client folder based on 'lastName, firstName'

I've been able to write a simple program to move files between folders, but not one that will do the aforesaid name matching.

shutil.copy('C:/Users/Kenny/Documents/Scan_Drive','C:/Users/Kenny/Documents/Clients')

^ Moving a file from one folder to another.. quite easily done.

Is there a way to modify the above code to apply to a regex (below)?

shutil.copy('C:/Users/Kenny/Documents/Scan_Drive/\w*', 'C:/Users/Kenny/Documents/Clients/\w*')

EDIT: @Byberi - Something as such?

path = "C:/Users/Kenny/Documents/Scans" dirs = os.path.isfile()

This would print all the files and directories

for file in dirs: print file

dest_dir = "C:/Users/Kenny/Documents/Clients" for file in glob.glob(r'C:/*'): print(file) shutil.copy(file, dest_dir)

I've consulted the following threads already, but I cannot seem to find how to match and move the files.

Select files in directory and move them based on text list of filenames

https://docs.python.org/3/library/glob.html

Python move files from directories that match given criteria to new directory

https://www.guru99.com/python-copy-file.html

https://docs.python.org/3/howto/regex.html

https://code.tutsplus.com/tutorials/file-and-directory-operations-using-python--cms-25817

0条回答
登录 后发表回答