![](/img/trans.png)
[英]Listing all directories and files in a sub-directory not working - Python
[英]Python move all files in directory to sub-directory with timestamp in another directory
假設您的腳本與Processed
和Unprocessed
目錄位於同一文件夾中,您可以這樣做:
import os, shutil, datetime
UNPROCESSED_PATH = 'Unprocessed'
PROCESSED_PATH = 'Processed'
try:
filesToMove = os.listdir(UNPROCESSED_PATH)
except FileNotFoundError:
print(f"No '{UNPROCESSED_PATH}' directory found")
exit()
if len(filesToMove) == 0:
print('No files to process')
exit()
currTime = datetime.datetime.now()
currTimeStr = currTime.strftime("%d%m%y_%H%M%S")
newDirPath = f'{PROCESSED_PATH}/Pr_{currTimeStr}'
os.mkdir(newDirPath)
print(f'Created {newDirPath} directory')
for file in filesToMove:
shutil.move(f'{UNPROCESSED_PATH}/{file}', f'{newDirPath}/{file}')
print(f'Moving {file}')
print(f'Done processing {len(filesToMove)} files')
在Windows Pro
上使用Python 3.6.4
進行測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.