资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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中怎么创建目录
URL分享:http://cdkjz.cn/article/ghocgg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220