Python中怎么创建目录

Python中怎么创建目录,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联是一家专注于成都网站建设、网站设计与策划设计,自流井网站建设哪家好?创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:自流井等地区。自流井做网站价格咨询:18982081108

首先,列出一个目录的内容:

view plaincopy to clipboardprint?  import os   for fileName in os.listdir ( '/' ):   print fileName   import os  for fileName in os.listdir ( '/' ):

print fileName正如你所见,这很简单,用三行代码就可以完成。
创建目录也很简单:

view plaincopy to clipboardprint?  import os   for fileName in os.listdir ( '/' ):   print fileName   import os  for fileName in os.listdir ( '/' ):

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

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

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

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”文件的内容和“.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中怎么创建目录问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。

分享文章:Python中怎么创建目录
网址分享:https://www.cdcxhl.com/article16/ghocgg.html

成都网站建设公司_创新互联,为您提供用户体验自适应网站搜索引擎优化外贸网站建设全网营销推广电子商务

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

绵阳服务器托管