Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果数据库中存在文件夹A中的文件名,请移动到文件夹B;如果文件不存在,请将文件名插入数据库并将文件移动到文件夹C_Python - Fatal编程技术网

Python 如果数据库中存在文件夹A中的文件名,请移动到文件夹B;如果文件不存在,请将文件名插入数据库并将文件移动到文件夹C

Python 如果数据库中存在文件夹A中的文件名,请移动到文件夹B;如果文件不存在,请将文件名插入数据库并将文件移动到文件夹C,python,Python,我有3个文件夹 Folder A (Landing Folder) Folder B (Deletion Folder) Folder C (Transfer to AWS Folder) 我已经编写了一些python代码,用于在一些数据转换之后将文件从着陆文件夹移动到着陆文件夹-我需要为文件创建一个决策树: Check each filename in folder A to see if it exists in the database if the file exists alre

我有3个文件夹

Folder A (Landing Folder)
Folder B (Deletion Folder)
Folder C (Transfer to AWS Folder)
我已经编写了一些python代码,用于在一些数据转换之后将文件从着陆文件夹移动到着陆文件夹-我需要为文件创建一个决策树:

 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 
我对python中的SQL语句进行了测试并正常工作,但我不确定如何将文件移动到各自的文件夹中

我假设在使用shutil.move到文件夹B或C的过程中需要一些东西,这取决于结果,但不是100%确定要达到目的的语法

谢谢你的帮助

您可以使用os.listdir来获取给定路径中的文件列表,正如您所说的,您可以使用shutil.move来移动文件。因此,您可以尝试以下方法:

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)

我忽略了数据库中fil和数据库中insertfil步骤的明确性,因为您说过已经成功测试了SQL语句。您可以勾选关于shutil和os.listdir:,,。

我刚刚编辑了我的答案,希望它能有所帮助。@MrNobody33-是的,答案很棒,我可以从中构建一些东西!谢谢你的邀请assist@MrNoboddy33看起来我可能有点超前了-我可以很好地执行插入查询-但是在检查数据库中是否已经存在文件名方面运气不佳,如果您能帮助完成这一部分,我将非常感谢您。希望它能帮助你。