copy_rename_file

# -*- 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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值