Python中怎么創(chuàng)建目錄,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設、網(wǎng)站設計與策劃設計,自流井網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設十年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:自流井等地區(qū)。自流井做網(wǎng)站價格咨詢:18982081108
首先,列出一個目錄的內(nèi)容:
view plaincopy to clipboardprint? import os for fileName in os.listdir ( '/' ): print fileName import os for fileName in os.listdir ( '/' ):
print fileName正如你所見,這很簡單,用三行代碼就可以完成。
創(chuàng)建目錄也很簡單:
view plaincopy to clipboardprint? import os for fileName in os.listdir ( '/' ): print fileName import os for fileName in os.listdir ( '/' ):
刪除剛才創(chuàng)建的Python目錄:也可以創(chuàng)建多級目錄:
view plaincopy to clipboardprint? import os os.makedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes' ) import os os.makedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes' )
如果沒有在創(chuàng)建的文件夾中添加任何東西,就可以一次性將它們?nèi)縿h除(即,刪除所列的所有空文件夾):
view plaincopy to clipboardprint? import os os.removedirs( 'I/will/show/you/how/deep/the/rabbit/hole/goes' ) import os os.removedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes' )
當需要對一個特定的文件類型進行操作時,我們可以選擇“fnmatch”模塊。以下是顯示“.txt”文件的內(nèi)容和“.exe”文件的文件名:
view plaincopy to clipboardprint? import fnmatch import os for fileName in os.listdir ( '/' ): if fnmatch.fnmath ( fileName, '*.txt' ): print open ( fileName ).read() elif fnmatch.fnmatch ( fileName, '*.exe' ): print fileName import fnmatch import os for fileName in os.listdir ( '/' ): if fnmatch.fnmath ( fileName, '*.txt' ): print open ( fileName ).read() elif fnmatch.fnmatch ( fileName, '*.exe' ): print fileName“*”
字符可以表示任意長度的字符。如果要匹配一個字符,則使用“?”符號:
view plaincopy to clipboardprint? import fnmatch import os for fileName in os.listdir ( '/' ): if fnmatch.fnmatch ( fileName, '?.txt' ): print 'Text file.' import fnmatch import os for fileName in os.listdir ( '/' ): if fnmatch.fnmatch ( fileName, '?.txt' ): print 'Text file.'“fnmatch”
關于Python中怎么創(chuàng)建目錄問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關知識。
分享文章:Python中怎么創(chuàng)建目錄
網(wǎng)址分享:http://chinadenli.net/article16/ghocgg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供用戶體驗、自適應網(wǎng)站、搜索引擎優(yōu)化、外貿(mào)網(wǎng)站建設、全網(wǎng)營銷推廣、電子商務
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)