用Python目录创建及应用

开发 后端
这篇文章主要介绍的是Python目录的操作的具体介绍,还有通过Python目录可以创建多级目录,的相关操作技巧的详细介绍。

Python目录是计算机语言常用的语言,可是很多人还是对其不太了解,觉得它的实际应用语言操作太难,其实如果你了解了Python目录的操作技能,你对其就有个更深的了解。你如果感兴趣的话,就看看下面的文章吧!

和普通文件一样,关于Python目录的操作也很容易掌握。首先,列出一个目录的内容:

  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正如你所见,这很简单,用三行代码就可以完成。
创建目录也很简单:

  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.  

删除刚才创建的Python目录:也可以创建多级目录:

  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' )  

如果没有在创建的文件夹中添加任何东西,就可以一次性将它们全部删除(即,删除所列的所有空文件夹):

  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' )  

当需要对一个特定的文件类型进行操作时,我们可以选择“fnmatch”模块。以下是显示“.txt”文件的内容和“.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“*”  

字符可以表示任意长度的字符。如果要匹配一个字符,则使用“?”符号:

  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”  

以上就是对关于Python目录具体操作内容的简介。

【编辑推荐】

  1. Python正则表达式十种相关的匹配方法
  2. Python字符串替换如何才能进行字符的拆分
  3. Python文本乱码发生时的解决方案
  4. Python编程语言总体性能优点评测
  5. Python输入方式具体的三种实现方式
责任编辑:佚名 来源: IT专家网
相关推荐

2010-03-24 08:55:02

Python编程语言

2010-03-16 18:19:41

Python函数

2010-02-07 14:57:37

Ubuntu samb

2014-03-19 19:46:43

Linuxmkdir 命令

2010-01-13 09:52:38

Linuxdebian

2017-09-13 09:23:04

磁盘分区bash

2009-01-19 11:07:42

C#Web.NET

2023-05-10 08:05:41

GoWeb应用

2010-01-18 17:09:52

VB.NET创建虚拟目

2011-05-17 10:04:18

Python

2020-12-02 09:54:26

Django

2022-06-07 07:21:19

Python内置库命令行

2015-04-22 11:29:45

PythonPython创建瀑布图

2021-02-05 15:50:27

PythonShell命令

2011-07-20 13:04:58

XCode XCode 4 ipa

2011-11-17 16:26:49

AndroidAdobeAIR

2009-09-28 10:28:04

Linux删除目录子目录

2010-03-23 14:54:27

Python目录文件

2016-11-11 20:54:37

2012-05-13 12:43:50

iOS
点赞
收藏

51CTO技术栈公众号