Python 3-复制文件如果目标文件夹中不存在

问题描述

我正在尝试将数千个pdf从一个文件位置移动到另一个文件位置。源文件夹包含多个子文件夹,我仅将pdf(技术图纸)组合到一个文件夹中,以简化对团队其他成员的搜索。

主要目标是仅复制目标文件夹中尚不存在的文件。我尝试了几个不同的选项,最近一次显示如下,在所有情况下,每次都会复制每个文件。在今天之前,无论何时我尝试移动批量文件,如果该文件存在于目标文件夹中,但我不再这样做,则会收到错误消息。

我已验证两个位置都存在某些文件,但仍在复制中。有什么我想念的东西或者可以修改以纠正吗?

感谢您的帮助。

import os.path
import shutil

source_folder = os.path.abspath(r'\\source\file\location')

dest_folder = os.path.abspath(r'\\dest\folder\location')

    for folder,subfolders,files in os.walk(source_folder):
        for file in files:
            path_file=os.path.join(folder,file)
            if os.path.exists(file) in os.walk(dest_folder):
                print(file+" exists.")
            if not os.path.exists(file) in os.walk(dest_folder):
                  print(file+' does not exist.')
                  shutil.copy2(path_file,dest_folder)

解决方法

DateUtils返回一个布尔值。 #include <SysUtils.hpp> namespace fixed { TDateTime __fastcall IncSecond(const TDateTime AValue,const __int64 ANumberOfSeconds = 1) { TTimeStamp TS = DateTimeToTimeStamp(AValue); double TempTime = TimeStampToMSecs(TS); // if the above call to TimeStampToMSecs() proves to be inaccurate (it did // in my test in C++,but worked fine in Delphi),you can use this instead: // double TempTime = (double(TS.Date) * double(MSecsPerDay)) + double(TS.Time); TempTime = TempTime + (ANumberOfSeconds * MSecsPerSec); TS = MSecsToTimeStamp(TempTime); return TimeStampToDateTime(TS); } } TDateTime dt(2000,1,0); AnsiString sdt = ""; DateTimeToString(sdt,"yyyy/mm/dd hh:nn:ss",dt); closeDateTime = dt; closeDateTime = fixed::IncSecond(closeDateTime,footer->secondsFromZeroDateOfFinishDocument); DateTimeToString(sdt,closeDateTime); 创建一个生成器,生成形式为os.path.exists的三元组。因此,第一个条件永远不会成立。

此外,即使该条件是正确的,您的第二个条件也具有冗余性,因为它仅仅是对第一个条件的否定。您可以将其替换为os.walk

您想要的是类似的东西

(dirpath,dirnames,filenames)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...