日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python文件路徑具體操作方法經典講解

Python編程語言在實際使用中可以幫助我們輕松的實現(xiàn)一些特殊的功能需求。在這里我們將會為大家詳細介紹一下有關Python文件路徑的相關操作技巧,從而方便我們在實際開發(fā)中獲得一些幫助。

Python文件路徑操作方法之1: os.listdir(path) //path為目錄

功能相當于在path目錄下執(zhí)行dir命令,返回為list類型。舉例:

 
 
 
  1. print os.listdir(’..’) 

輸出:

 
 
 
  1. [a,b,c,d] 

Python文件路徑操作方法之2: os.path.walk(path,visit,arg)

path :是將要遍歷的目錄

visit :是一個函數(shù)指針,函數(shù)圓形為:

 
 
 
  1. callback(arg,dir,fileList) 

其中arg為為傳給walk的arg , dir是path下的一個目錄,fileList為dir下的文件和目錄組成的list

arg:傳給visit用的,對walk沒有什么作用

舉例:

 
 
 
  1. def callback(arg,directory, files):  
  2. print directory,  
  3. print files,  
  4. print arg  
  5. print ‘——————–’  
  6. os.path.walk(’.',callback, ‘123456′) 

輸出:

 
 
 
  1. . ['path0704.py', 'temp', '\xc2\xb7\xbe\xb6\xcf\xe0\xb9\
    xd8\xd1\xa7\xcf\xb0.txt'] 123456  
  2. ——————–  
  3. .\temp ['temp.h', 'temp1'] 123456  
  4. ——————–  
  5. .\temp\temp1 ['abc.bmp'] 123456 

如果想找到某個目錄下所有文件,只需要在callback里面,在fileList中找出文件,即可

除此之外,還有一個函數(shù)可以用那就是os.walk,看10

Python文件路徑操作方法之3:os.path.split(path)

path 為一個路徑,輸出,把path分成兩部分,具體看實例:

 
 
 
  1. print os.path.split(”abc/de.txt”)  
  2. (’abc’, ‘de.txt’)  
  3. os.path.split(”abc”)  
  4. (”, ‘abc’)  
  5. print os.path.split(”de/abc/de”)  
  6. (’de/abc’, ‘de’) 

Python文件路徑操作方法之4: os.path.splitext(filename)

把文件名分成文件名稱和擴展名

 
 
 
  1. os.path.splitext(abc/abcd.txt)  
  2. (’abc/abcd’, ‘.txt’) 

Python文件路徑操作方法之5: os.path.dirname(path)

把目錄名提出來

 
 
 
  1. print os.path.dirname(”abc”)  
  2. #輸出為空  
  3. print os.path.dirname(’abc\def’)  
  4. abc 

Python文件路徑操作方法之6: os.path.basename(filename)

取得主文件名

 
 
 
  1. print os.path.basename(’abc’)  
  2. abc  
  3. print os.path.basename(’abc.txt’)  
  4. abc  
  5. print os.path.basename(’bcd/abc’)  
  6. abc #這個需要注意不包括目錄名稱  
  7. print os.path.basename(’.') 

Python文件路徑操作方法之7:os.mkdir(path, [mode])#t#

path為目錄名: 這里有個要求,只能創(chuàng)建一級目錄。比如path為 abc/def 則當前目錄下必須存在abc 否則失敗

Python文件路徑操作方法之8: os.makedirs(path [,mode])

可以創(chuàng)建多級目錄

Python文件路徑操作方法之9:os.remove(path)

刪除一個文件,一定是一個文件

 
 
 
  1. os.removedirs(path) 刪除一個目錄下所有東西  
  2. os.rmdir(path) 刪除一個目錄,而且一定要空,否則os.errer 

Python文件路徑操作方法之10:os.walk(path)

遍歷path,返回一個對象,他的每個部分都是一個三元組(’目錄x’,[目錄x下的目錄list],目錄x下面的文件)

舉例:

 
 
 
  1. a = os.walk(’.')  
  2. for i in a:  
  3. print i 

輸出:

 
 
 
  1. (’.', ['abc', 'temp'], ['path0704.py', '\xc2\xb7\xbe\xb6\xcf\
    xe0\xb9\xd8\xd1\xa7\xcf\xb0.txt'])  
  2. (’.\\abc’, [], ['\xd0\xc2\xbd\xa8 BMP \xcd\xbc\xcf\xf1.bmp'])  
  3. (’.\\temp’, ['temp1'], ['temp.h'])  
  4. (’.\\temp\\temp1′, [], ['abc.bmp']) 

Python文件路徑操作方法之11:shutil.copy(src,dst)

把文件src內容拷貝到文件dst中。,目標區(qū)域必須可以寫,如果dst存在,則dst被覆蓋

上面的Python文件路徑的函數(shù)基本夠用

其它文件移動操作還請看:shutil模塊:High-level file operations


網(wǎng)頁題目:Python文件路徑具體操作方法經典講解
URL標題:http://m.5511xx.com/article/dppdppi.html