偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

用Python目錄創(chuàng)建及應(yīng)用

開發(fā) 后端
這篇文章主要介紹的是Python目錄的操作的具體介紹,還有通過Python目錄可以創(chuàng)建多級目錄,的相關(guān)操作技巧的詳細(xì)介紹。

Python目錄是計(jì)算機(jī)語言常用的語言,可是很多人還是對其不太了解,覺得它的實(shí)際應(yīng)用語言操作太難,其實(shí)如果你了解了Python目錄的操作技能,你對其就有個(gè)更深的了解。你如果感興趣的話,就看看下面的文章吧!

和普通文件一樣,關(guān)于Python目錄的操作也很容易掌握。首先,列出一個(gè)目錄的內(nèi)容:

  1. view plaincopy to clipboardprint?  
  2. import os   
  3. for fileName in os.listdir ( '/' ):   
  4. print fileName   
  5. import os  
  6. for fileName in os.listdir ( '/' ):  
  7.  

print fileName正如你所見,這很簡單,用三行代碼就可以完成。
創(chuàng)建目錄也很簡單:

  1. view plaincopy to clipboardprint?  
  2. import os   
  3. for fileName in os.listdir ( '/' ):   
  4. print fileName   
  5. import os  
  6. for fileName in os.listdir ( '/' ):  
  7.  

刪除剛才創(chuàng)建的Python目錄:也可以創(chuàng)建多級目錄:

  1. view plaincopy to clipboardprint?  
  2. import os   
  3. os.makedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes' )   
  4. import os  
  5. os.makedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes' )  

如果沒有在創(chuàng)建的文件夾中添加任何東西,就可以一次性將它們?nèi)縿h除(即,刪除所列的所有空文件夾):

  1. view plaincopy to clipboardprint?  
  2. import os   
  3. os.removedirs( 'I/will/show/you/how/deep/the/rabbit/hole/goes' )   
  4. import os  
  5. os.removedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes' )  

當(dāng)需要對一個(gè)特定的文件類型進(jìn)行操作時(shí),我們可以選擇“fnmatch”模塊。以下是顯示“.txt”文件的內(nèi)容和“.exe”文件的文件名:

  1. view plaincopy to clipboardprint?  
  2. import fnmatch   
  3. import os   
  4. for fileName in os.listdir ( '/' ):   
  5. if fnmatch.fnmath ( fileName, '*.txt' ):   
  6. print open ( fileName ).read()   
  7. elif fnmatch.fnmatch ( fileName, '*.exe' ):   
  8. print fileName   
  9. import fnmatch  
  10. import os  
  11. for fileName in os.listdir ( '/' ):  
  12. if fnmatch.fnmath ( fileName, '*.txt' ):  
  13. print open ( fileName ).read()  
  14. elif fnmatch.fnmatch ( fileName, '*.exe' ):  
  15. print fileName“*”  

字符可以表示任意長度的字符。如果要匹配一個(gè)字符,則使用“?”符號:

  1. view plaincopy to clipboardprint?  
  2. import fnmatch   
  3. import os   
  4. for fileName in os.listdir ( '/' ):   
  5. if fnmatch.fnmatch ( fileName, '?.txt' ):   
  6. print 'Text file.'   
  7. import fnmatch  
  8. import os  
  9. for fileName in os.listdir ( '/' ):  
  10. if fnmatch.fnmatch ( fileName, '?.txt' ):  
  11. print 'Text file.'“fnmatch”  

以上就是對關(guān)于Python目錄具體操作內(nèi)容的簡介。

【編輯推薦】

  1. Python正則表達(dá)式十種相關(guān)的匹配方法
  2. Python字符串替換如何才能進(jìn)行字符的拆分
  3. Python文本亂碼發(fā)生時(shí)的解決方案
  4. Python編程語言總體性能優(yōu)點(diǎn)評測
  5. Python輸入方式具體的三種實(shí)現(xiàn)方式
責(zé)任編輯:佚名 來源: IT專家網(wǎng)
相關(guān)推薦

2010-03-24 08:55:02

Python編程語言

2010-03-16 18:19:41

Python函數(shù)

2010-02-07 14:57:37

Ubuntu samb

2010-01-13 09:52:38

Linuxdebian

2017-09-13 09:23:04

磁盤分區(qū)bash

2014-03-19 19:46:43

Linuxmkdir 命令

2009-01-19 11:07:42

C#Web.NET

2023-05-10 08:05:41

GoWeb應(yīng)用

2010-01-18 17:09:52

VB.NET創(chuàng)建虛擬目

2020-12-02 09:54:26

Django

2011-05-17 10:04:18

Python

2011-07-20 13:04:58

XCode XCode 4 ipa

2011-11-17 16:26:49

AndroidAdobeAIR

2022-06-07 07:21:19

Python內(nèi)置庫命令行

2021-02-05 15:50:27

PythonShell命令

2015-04-22 11:29:45

PythonPython創(chuàng)建瀑布圖

2009-09-28 10:28:04

Linux刪除目錄子目錄

2012-05-02 23:04:38

iOS

2012-05-13 12:43:50

iOS

2011-04-19 10:38:53

Xcode 4MacRubyiOS
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號