首页 > 解决方案 > 使用带有字典值的 .find 来组织文件

问题描述

import os
import shutil

# path to downloads
downloadPath = 'C:\\Users\\USER\\Downloads\\'

# creates a list of all download files
downloadFileList = os.listdir(downloadPath)

# path to companies
companyPath = 'C:\\Users\\USER\\Documents\\Companies\\'

# creates a list of the company folders
companyFolderList = os.listdir(companyPath)

# creates a list of company folder addresses
CompanyDict = {'Company': 'Path'}

# Make a dictionary that has the company key and path values
for company in companyFolderList:
    CompanyDict.update({company: f'C:\\Users\\USER\\Documents\\Companies\\' + company})

print(CompanyDict)

for download in downloadFileList:
    if download.find(str(CompanyDict.items())) >= 1:
                shutil.move(f'C:\\Users\\USER\\Downloads' + download, f'C:\\Users\\USER\\Documents\\Companies' + download)
                print(download + ' has been moved into the Documents Folder')
    else:
        print(download + " has not been moved")

因此,我正在尝试制作一个程序,该程序将查看我的下载文件夹中是否有包含特定公司名称的文档,然后如果有匹配项,则将它们移动到公司文件夹。

发生的事情是 for 循环和 if 函数似乎没有拾取我留在下载文件夹中的一些文件作为测试。例如,其中一个输出是

Canon TEST.csv has not been moved

即使我的公司文件夹中有一个佳能文件夹。

任何帮助,将不胜感激

标签: pythonsortingdictionaryshutil

解决方案


推荐阅读