2017-06-23 37 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 
相关问题