最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2016年04月19日 08:48:16   投稿:hebedich  
本文通過2個具體的實例,給大家展示了如何使用Python實現(xiàn)文件的復(fù)制與刪除,非常的簡單實用,有需要的小伙伴可以參考下

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

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

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

首先插入必要的庫:

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

然后就是一大堆功能函數(shù)。第一個就是把某一目錄下的所有文件復(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)

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

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

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

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)

往指定目錄寫文本文件:

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

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

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)了:

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"

    感覺是果然簡單, 不過簡單的原因是因為庫函數(shù)豐富,語言基本特性的簡單真沒感覺出來。

我們再來看一個實例

本人一直用foobar2000作為音樂播放器,聽歌時候把自己喜歡的歌都會特別添加到一個播放列表。

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

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

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

直接貼代碼吧,寫得比較倉促,各位將就參考下即可:

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

相關(guān)文章

  • 詳解pycharm配置python解釋器的問題

    詳解pycharm配置python解釋器的問題

    這篇文章主要介紹了安裝好Pycharm后如何配置Python解釋器簡易教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 使用python實現(xiàn)滑動驗證碼功能

    使用python實現(xiàn)滑動驗證碼功能

    這篇文章主要介紹了使用python實現(xiàn)滑動驗證碼功能,本文通過示例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-08-08
  • python入門前的第一課 python怎樣入門

    python入門前的第一課 python怎樣入門

    人工智能這么火,0基礎(chǔ)能學(xué)python嗎?python該怎么選擇編輯器?怎么搭建python運行環(huán)境?python好學(xué)嗎,怎么學(xué)?這是所有python入門前同學(xué)都會提出的疑問,這篇文章和大家一起學(xué)習(xí)python,感興趣的小伙伴們可以加入
    2018-03-03
  • 如何在Django項目中引入靜態(tài)文件

    如何在Django項目中引入靜態(tài)文件

    這篇文章主要介紹了如何在Django項目中引入靜態(tài)文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • python3 字符串知識點學(xué)習(xí)筆記

    python3 字符串知識點學(xué)習(xí)筆記

    字符串是 Python 中最常用的數(shù)據(jù)類型。我們可以使用引號('或")來創(chuàng)建字符串
    2020-02-02
  • python數(shù)據(jù)庫如何連接SQLite詳解

    python數(shù)據(jù)庫如何連接SQLite詳解

    這篇文章主要介紹了Python實現(xiàn)連接SQLite數(shù)據(jù)庫的方法,在Python數(shù)據(jù)庫編程中有著廣泛的應(yīng)用,需要的朋友可以參考下,希望能給你帶來幫助
    2021-08-08
  • Python中最強(qiáng)大的重試庫Tenacity使用探索

    Python中最強(qiáng)大的重試庫Tenacity使用探索

    這篇文章主要為大家介紹了Python中最強(qiáng)大的重試庫Tenacity使用探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • 詳解讓Python性能起飛的15個技巧

    詳解讓Python性能起飛的15個技巧

    Python?一直以來被大家所詬病的一點就是執(zhí)行速度慢,但不可否認(rèn)的是?Python?依然是我們學(xué)習(xí)和工作中的一大利器。本文總結(jié)了15個tips有助于提升?Python?執(zhí)行速度、優(yōu)化性能,需要的可以參考一下
    2022-02-02
  • python numpy函數(shù)中的linspace創(chuàng)建等差數(shù)列詳解

    python numpy函數(shù)中的linspace創(chuàng)建等差數(shù)列詳解

    numpy.linspace是用于創(chuàng)建一個一維數(shù)組,并且是等差數(shù)列構(gòu)成的一維數(shù)組,下面這篇文章主要給大家介紹了關(guān)于python numpy函數(shù)中的linspace創(chuàng)建等差數(shù)列的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2017-10-10
  • 關(guān)于Python 位運算防坑指南

    關(guān)于Python 位運算防坑指南

    這篇文章主要介紹了關(guān)于Python 位運算防坑指南,小編將劇烈向大家說明并且列舉python及C#兩種語言,需要的朋友可以參考下面文章的具體內(nèi)容
    2021-09-09

最新評論

南昌县| 团风县| 秦皇岛市| 阳东县| 巴塘县| 沐川县| 师宗县| 大悟县| 建阳市| 平遥县| 宜兰县| 高州市| 新兴县| 淮北市| 泽普县| 汝南县| 长宁区| 通海县| 达拉特旗| 文水县| 阳朔县| 五常市| 威信县| 长宁区| 小金县| 博客| 佛山市| 临漳县| 蓬莱市| 收藏| 大冶市| 武山县| 宿州市| 德江县| 丰宁| 苍溪县| 台中市| 定州市| 抚远县| 西平县| 常德市|