【发布时间】:2020-09-05 17:10:48
【问题描述】:
我在 Source 中有 3 个文件夹(A、B 和 C)。每个文件夹至少包含 1 个文件。我想在每个文件夹中找到最新的文件并将其发送到还包含文件夹 A、B 和 C 的目标。不是最新的文件将被移动到存档,其中还包含文件夹 A、B 和 C。我使用了下面的代码,但我收到以下错误:NotADirectoryError: [WinError 267] The directory name is invalid: 'c:\\data\\AS\\Desktop\\Source\\A\\12.txt'
这是我的代码:
from datetime import datetime,timedelta
import shutil, os, os.path
import time
#Make Source, Destination and Archive paths.
source = r'c:\data\AS\Desktop\Source'
destination = r'c:\data\AS\Desktop\Destination'
archive = r'c:\data\AS\Desktop\Archive'
#First os.walk for the source folder itself
for root, dirs, files in os.walk(source):
for folder in dirs:
subdir=root+'\\'+folder
#second os.walk for each folder in the source folder (A, B, and C)
for subroot, subdirs, subfiles in os.walk(subdir):
for file in subfiles:
filePath=subroot+'\\'+file
maxi = max(os.listdir(filePath), key=os.path.getctime)
print(maxi)
我也想知道 key=os.path.getctime 中的 key 代表什么。先谢谢大家了
【问题讨论】:
标签: python file file-management