1. 程式人生 > 程式設計 >python3檔案複製、延遲檔案複製任務的實現方法

python3檔案複製、延遲檔案複製任務的實現方法

使用python版本3.6.1

工作中測試客戶端傳輸報文速率,寫了以下兩個指令碼。

第一個,簡單的複製檔案並重命名。

第二個,在迴圈中增加延時的功能。

使用場景將檔案複製並重命名(重新命名方式在檔案末尾加生成的隨機數)

#!/usr/bin/python3
#coding=GB2312
import os
import os.path
import random
import shutil
count = 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=GB2312
import os
import os.path
import random
import 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," 複製任務完成")

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對我們的支援。