查看文件名和文件路徑
>>> import os>>> url = 'http://images.cnitblog.com/i/311516/201403/020013141657112.png'>>> filename = os.path.basename(url)>>> filepath = os.path.dirname(url)>>> filename '020013141657112.png'>>> filepath'http://images.cnitblog.com/i/311516/201403'>>>
import os print(os.path.realpath(__file__)) # 當(dāng)前文件的路徑 print(os.path.dirname(os.path.realpath(__file__))) # 從當(dāng)前文件路徑中獲取目錄 print(os.path.basename(os.path.realpath(__file__))) # 從當(dāng)前文件路徑中獲取文件名
print(os.listdir(dirname)) # 只顯示該目錄下的文件名和目錄名,不包含子目錄中的文件,默認(rèn)為當(dāng)前文件所在目錄
import os# os.walk()遍歷文件夾下的所有文件# os.walk()獲得三組數(shù)據(jù)(rootdir, dirname,filnames)def file_path(file_dir): for root, dirs, files in os.walk(file_dir): print(root, end=' ') # 當(dāng)前目錄路徑 print(dirs, end=' ') # 當(dāng)前路徑下的所有子目錄 print(files) # 當(dāng)前目錄下的所有非目錄子文件
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持武林站長(zhǎng)站!
新聞熱點(diǎn)
疑難解答
圖片精選