激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Python復制文件操作實例詳解

Python復制文件操作實例詳解

2020-08-02 10:29一克棉花 Python

這篇文章主要介紹了Python復制文件操作的方法,涉及Python針對文件與目錄的復制及刪除操作相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了Python復制文件操作用法。分享給大家供大家參考,具體如下:

這里用python實現(xiàn)了一個小型的自動發(fā)版本的工具。這個“自動發(fā)版本”有點虛, 只是簡單地把debug 目錄下的配置文件復制到指定目錄,把Release下的生成文件復制到同一指定,過濾掉不需要的文件夾(.svn),然后再往這個指定目錄添加幾個特定的文件。

這個是我的第一個python小程序。

下面就來看其代碼的實現(xiàn)。

首先插入必要的庫:

?
1
2
3
4
import os
import os.path
import shutil
import time, datetime

然后就是一大堆功能函數(shù)。第一個就是把某一目錄下的所有文件復制到指定目錄中:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
def copyFiles(sourceDir, targetDir):
   if sourceDir.find(".svn") > 0:
     return
   for file in os.listdir(sourceDir):
     sourceFile = os.path.join(sourceDir, file)
     targetFile = os.path.join(targetDir, file)
     if os.path.isfile(sourceFile):
       if not os.path.exists(targetDir):
         os.makedirs(targetDir)
       if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):
           open(targetFile, "wb").write(open(sourceFile, "rb").read())
     if os.path.isdir(sourceFile):
       First_Directory = False
       copyFiles(sourceFile, targetFile)

刪除一級目錄下的所有文件:

?
1
2
3
4
5
def removeFileInFirstDir(targetDir):
   for file in os.listdir(targetDir):
     targetFile = os.path.join(targetDir, file)
     if os.path.isfile(targetFile):
       os.remove(targetFile)

復制一級目錄下的所有文件到指定目錄:

?
1
2
3
4
5
6
7
def coverFiles(sourceDir, targetDir):
     for file in os.listdir(sourceDir):
       sourceFile = os.path.join(sourceDir, file)
       targetFile = os.path.join(targetDir, file)
       #cover the files
       if os.path.isfile(sourceFile):
         open(targetFile, "wb").write(open(sourceFile, "rb").read())

復制指定文件到目錄:

?
1
2
def moveFileto(sourceDir, targetDir):
  shutil.copy(sourceDir, targetDir)

往指定目錄寫文本文件:

?
1
2
def writeVersionInfo(targetDir):
  open(targetDir, "wb").write("Revison:")

返回當前的日期,以便在創(chuàng)建指定目錄的時候用:

?
1
2
3
4
5
6
7
8
9
10
def getCurTime():
   nowTime = time.localtime()
   year = str(nowTime.tm_year)
   month = str(nowTime.tm_mon)
   if len(month) < 2:
     month = '0' + month
   day = str(nowTime.tm_yday)
   if len(day) < 2:
     day = '0' + day
   return (year + '-' + month + '-' + day)

然后就是主函數(shù)的實現(xiàn)了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if __name__ =="__main__":
   print "Start(S) or Quilt(Q) \n"
   flag = True
   while (flag):
     answer = raw_input()
     if 'Q' == answer:
       flag = False
     elif 'S'== answer :
       formatTime = getCurTime()
       targetFoldername = "Build " + formatTime + "-01"
       Target_File_Path += targetFoldername
       copyFiles(Debug_File_Path,  Target_File_Path)
       removeFileInFirstDir(Target_File_Path)
       coverFiles(Release_File_Path, Target_File_Path)
       moveFileto(Firebird_File_Path, Target_File_Path)
       moveFileto(AssistantGui_File_Path, Target_File_Path)
       writeVersionInfo(Target_File_Path+"\\ReadMe.txt")
       print "all sucess"
     else:
       print "not the correct command"

希望本文所述對大家python程序設(shè)計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久久一区二区三区精品 | 一级黄色淫片 | 99在线精品视频免费观看20 | 久久久久北条麻妃免费看 | 日韩字幕在线观看 | 亚洲狠狠入 | 正在播放91视频 | 成人在线观看一区 | 亚洲综合一区二区三区 | 婷婷久久青草热一区二区 | 国产精品美女久久久久久网站 | 女人叉开腿让男人桶 | 羞羞羞羞视频 | 黄色特级视频 | 91午夜在线观看 | 成人一区三区 | 亚洲一区二区 | 老a影视网站在线观看免费 国产精品久久久久久久久久尿 | 香蕉黄色网 | 国产亲子伦在线观看 | 偷偷草网站 | a网站在线 | 99re热视频这里只精品 | 午夜精品福利视频 | 日本aaaa片毛片免费观看视频 | 久久精品在线免费观看 | 老女人碰碰在线碰碰视频 | 无遮挡一级毛片视频 | 久久久久久久久久久久久国产精品 | 欧美日韩亚洲一区二区三区 | av在线一区二区三区四区 | 亚洲精品久久久久久 | 久色乳综合思思在线视频 | 久久99精品久久久久久秒播蜜臀 | 免费观看三级毛片 | 国产精品手机在线亚洲 | 99视频有精品视频高清 | 亚洲成人在线视频网 | 麻豆国产网站 | 成人偷拍片视频在线观看 | 大学生一级毛片在线视频 |