首页 > 编程 > Python > 正文

python3文件复制、延迟文件复制任务的实现方法

2019-11-25 11:47:22
字体:
来源:转载
供稿:网友

使用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+1 shutil.copyfile(src,tar) count = count + 1 #执行一次循环休眠时间(5秒) time.sleep(5)else: print (count, " 复制任务完成")

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对武林网的支持。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表