【问题标题】: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如果文件夹A中的文件名存在于数据库中移动到文件夹B,如果文件不存在,将文件名插入数据库并将文件移动到文件夹C
【发布时间】:2020-06-10 19:40:14
【问题描述】:

我有 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% 确定到达那里的语法。

任何帮助表示赞赏。

【问题讨论】:

  • 我刚刚编辑了我的答案,希望对您有所帮助。
  • @MrNobody33 - 是的,这个答案很棒,我可以从中构建一些东西!感谢您的协助

标签: python


【解决方案1】:

您可以使用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)

我忽略了步骤if fil in databaseinsert(fil) into database 的明确性,因为您说您已经成功测试了SQL 语句。您可以查看有关shutilos.listdir 的有用信息:link 1:shutillink 2:shutillink 3:shutillink 4:os.listdir

【讨论】:

  • @MrNoboddy33 似乎我可能有点超前了-我可以很好地执行插入查询-但是如果您可以帮忙,检查文件名是否已存在于数据库中的运气为零那部分,我将非常感激
  • 查看此link。希望对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多