2017-06-23 42 views

回答

0

首先,你需要在創建文件夾源文件夾,然後循環源文件夾中的所有文件,將它們移動到創建的dest文件夾。下面是例子:

import datetime 
import shutil 
import os 
now = datetime.datetime.today() 
nTime = now.strftime("%d-%m-%Y") 
source = '/home/ec2/files' 
dest = os.path.join(source+nTime) 
if not os.path.exists(dest): 
    os.makedirs(dest) #creat dest dir 

source_files = os.listdir(source) 
for f in source_files: 
    source_file = os.path.join(source,f) 
    if os.path.isfile(source_file): #check if source file is a file not dir 
     shutil.move(source_file,dest) #move all only files (not include dir) to dest dir 
相關問題