# -*- coding:utf-8 -*-
#version1.0
#python_version2.7
#拷贝文件到指定的目录,并重命名
import os
import sys
import shutil
import datetime
countFile=[0,]
def movefile(dir_path):
if os.path.exists(dir_path):
#获取文件所在的路径
filelist=os.listdir(dir_path)
#遍历文件,并copy到指定的路径下
for filename in filelist:
if os.path.isdir(dir_path + "\\" + filename):
# 移动所有文件到指定目录下面
src = dir_path + "\\" + filename
movefile(src)
else:
source_path=dir_path + "\\" +filename
target_path=target_pos + "\\" + filename
shutil.copyfile(source_path,target_path)
#文件数+1
countFile[0]+=1
def renameall(target):
#d定义时间变量
now=datetime.datetime.now()
today_date=now.strftime('%y%m%d')
#获得文件所在的路径
path_list=os.listdir(target)
print("before_change:" + str(path_list))
currentpath=os.getcwd()
os.chdir(target)
#遍历文件并使用rename()函数重命名
for each_path in path_list:
#将文件名拆分
tokens=each_path.split('.',1)
os.rename(each_path,(tokens[0]+today_date+'.'+tokens[1]))
print('----------------------')
os.chdir(currentpath)
sys.stdin.flush()
print("after_change:" + str(os.listdir(target)))
if __name__=='__main__':
# 需要复制文件的文件夹位置
source_pos = r"E:\scripts\test"
# 需要复制到的位置
target_pos = r"E:\scripts\EODfile"
print "------- 开始复制 ------"
movefile(source_pos)
print "将文件从****'", source_pos, "'复制到****'", target_pos, "'"
print "共复制了 ", countFile[0], " 个文件"
print "------ 复制完成 ------"
#修改指定目录下的文件名
renameall(target_pos)