python3 文件复制、延迟文件复制任务

来源:互联网 发布:倩女幽魂联赛对战算法 编辑:程序博客网 时间:2024/05/16 00:28

使用python版本3.6.1


工作中测试客户端传输报文速率,写了以下两个脚本。

第一个,简单的复制文件并重命名。
第二个,在循环中增加延时的功能。

使用场景将文件复制并重命名(重命名方式在文件末尾加生成的随机数)

#!/usr/bin/python3#coding=GB2312import osimport os.pathimport randomimport shutilcount = 0#源文件夹src="E:\\file\\CEB411Message__20171115123454.xml"#目标文件夹tar="E:\\file\\target4\\"while count < 10:    print (count, " 执行复制任务")    ram=str(random.randint(1,1000000))    tar="E:\\file\\target4\\"+"CEB411Message_74967F7C570E_"+ram+".xml"    count = count + 1    shutil.copyfile(src,tar)else:    print (count, " 复制任务完成")

此处,写为#coding=GB2312的原因是,在JetBrains PyCharm Community Edition 2017.1.2 x64 下utf-8运行正常,在win8 直接执行脚本时报错。这显然是字符集的问题,尝试后改为文中。

下面程序添加了一个循环,采用了引入延时生成。

#!/usr/bin/python3#coding=GB2312import osimport os.pathimport randomimport time  import shutil#源文件夹src="E:\\file\\xml\\311.xml"count = 0#总循环次数(10)while count <10:eachcount = 0#每次循环生成的条数(5)while eachcount <5:#生成随机数放在报文名中,用于区分报文名ram=str(random.randint(1,1000000000))tar="E:\\file\\xml\\3111\\"+"CEB411Message_116EA6A4-9D5A-4418-8281-74967F7C570E_"+ram+".xml"eachcount=eachcount+1shutil.copyfile(src,tar)count = count + 1#执行一次循环休眠时间(5秒)time.sleep(5)else:    print (count, " 复制任务完成")



原创粉丝点击