简体   繁体   中英

If filename in folder A exists in Database move to folder B, If file does not exist, insert filename into database and move file to folder C

I have 3 Folders

Folder A (Landing Folder)
Folder B (Deletion Folder)
Folder C (Transfer to AWS Folder)

I've written some python code to move files to the landing folder after some data transformation, from the landing folder - i need to make a decision tree for the files:

 Check each filename in folder A to see if it exists in the database
 if the file exists already:
    move the file to Folder B
 Else
 Insert the file name into the Database & move the file to Folder C for transfer to AWS 

I have the SQL statement inside python tested and working, but im not sure on how to move the files to their respective folders.

I'm assuming that i need something along the lines of using shutil.move to Folder B or C depending on outcome, but not 100% sure on the syntax to get there.

Any help appreciated.

You can use os.listdir to get a list of the files in a given path, and as you said, you can use shutil.move() to move the files. So, you could try something like this:

import os
import shutil

folderA='pathfolderA'
folderB='pathfolderB'
folderC='pathfolderC'

files=os.listdir(folderA)
for fil in files:
    if fil in database:
        shutil.move(fil,folderB)
    else:
        # insert(fil) into database
        shutil.move(fil,folderC)

I'm ignoring the explicity of steps if fil in database and insert(fil) into database , because you said you have the SQL statement successfully tested. You can chekout the this helpful info about shutil and os.listdir :link 1:shutil , link 2:shutil , link 3:shutil , link 4:os.listdir .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM