国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python實(shí)現(xiàn)文件復(fù)制刪除

2020-01-04 17:31:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
本文通過(guò)2個(gè)具體的實(shí)例,給大家展示了如何使用Python實(shí)現(xiàn)文件的復(fù)制與刪除,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下
 

 用python實(shí)現(xiàn)了一個(gè)小型的工具。其實(shí)只是簡(jiǎn)單地把debug 目錄下的配置文件復(fù)制到指定目錄,把Release下的生成文件復(fù)制到同一指定,過(guò)濾掉不需要的文件夾(.svn),然后再往這個(gè)指定目錄添加幾個(gè)特定的文件。

    這個(gè)是我的第一個(gè)python小程序。

    下面就來(lái)看其代碼的實(shí)現(xiàn)。

首先插入必要的庫(kù):

import os import os.path import shutil import time, datetime

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

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)

刪除一級(jí)目錄下的所有文件:

def removeFileInFirstDir(targetDir): for file in os.listdir(targetDir): targetFile = os.path.join(targetDir, file) if os.path.isfile(targetFile): os.remove(targetFile)

復(fù)制一級(jí)目錄下的所有文件到指定目錄:

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())

復(fù)制指定文件到目錄:

def moveFileto(sourceDir, targetDir): 
shutil.copy(sourceDir, targetDir)

往指定目錄寫(xiě)文本文件:

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

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

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ù)的實(shí)現(xiàn)了:

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"

    感覺(jué)是果然簡(jiǎn)單, 不過(guò)簡(jiǎn)單的原因是因?yàn)閹?kù)函數(shù)豐富,語(yǔ)言基本特性的簡(jiǎn)單真沒(méi)感覺(jué)出來(lái)。

我們?cè)賮?lái)看一個(gè)實(shí)例

本人一直用foobar2000作為音樂(lè)播放器,聽(tīng)歌時(shí)候把自己喜歡的歌都會(huì)特別添加到一個(gè)播放列表。

自己用iphone,同步歌曲的時(shí)候需要用到itunes,而itunes卻沒(méi)有我用foobar2000的精選播放列表呢~

本人只好定期把播放列表的mp3文件拷貝到一個(gè)目錄,我用itunes只需同步這個(gè)目錄即可
(順便吐槽下itunes不好使,在后期我都直接用其他同步工具代替之)

播放列表是*.m3u格式的文本,用記事本打開(kāi)可以看到mp3的絕對(duì)路徑。

直接貼代碼吧,寫(xiě)得比較倉(cāng)促,各位將就參考下即可:

#coding=gbk  import sys, shutil, os, string mp3List = "F://My Documents//mp3list//默認(rèn)精選.m3u" destDir = "G://POP//默認(rèn)精選"  def cpFile(srcPath):   fileName = os.path.basename(srcPath)   destPath = destDir + os.path.sep + fileName   if os.path.exists(srcPath) and not os.path.exists(destPath):     print 'cp %s %s' % (srcPath,destPath)     shutil.copy(srcPath,destPath)  if __name__ == '__main__':   f = file(mp3List, 'r')   lists = f.readlines()   for i in lists:     cpFile(string.strip(i))        f.close() 
 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新巴尔虎左旗| 贵定县| 沙田区| 方山县| 广元市| 黄浦区| 陇南市| 涟水县| 万全县| 鸡西市| 安丘市| 新野县| 闻喜县| 义乌市| 筠连县| 乌鲁木齐县| 阿克陶县| 临猗县| 汉阴县| 天门市| 章丘市| 台北市| 黔江区| 安图县| 胶州市| 冷水江市| 侯马市| 梅州市| 留坝县| 卓尼县| 鄂州市| 威远县| 扶风县| 巴马| 游戏| 白河县| 襄垣县| 云阳县| 兰坪| 惠州市| 钟祥市|