0
当前目录下有文件 /家庭/ EC2 /文件 ABC 高清 GHI蟒蛇如何创建一个目录,在当前目录中的时间戳和当前目录移动所有文件到newtimestamp目录
想创建一个使用目录下的n时间和移动所有文件到/ home/EC2 /文件/“n时间” 现在= datetime.datetime.today() n时间= now.strftime(格式)
当前目录下有文件 /家庭/ EC2 /文件 ABC 高清 GHI蟒蛇如何创建一个目录,在当前目录中的时间戳和当前目录移动所有文件到newtimestamp目录
想创建一个使用目录下的n时间和移动所有文件到/ home/EC2 /文件/“n时间” 现在= datetime.datetime.today() n时间= now.strftime(格式)
首先,你需要在创建文件夹源文件夹,然后循环源文件夹中的所有文件,将它们移动到创建的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