在Python中移动目录结构的方法

yipeiwu_com6年前Python基础

来源:http://stackoverflow.com/questions/3806562/ways-to-move-up-and-down-the-dir-structure-in-python

#Moving up/down dir structure
print os.listdir('.') # current level
print os.listdir('..') # one level up
print os.listdir('../..') # two levels up
 
# more complex example:
# This will walk the file system beginning in the directory the script is run from. It 
# deletes the empty directories at each level
 
for root, dirs, files in os.walk(os.getcwd()):
  for name in dirs:
    try:
      os.rmdir(os.path.join(root, name))
    except WindowsError:
      print 'Skipping', os.path.join(root, name)			

This will walk the file system beginning in the directory the script is run from. It deletes the empty directories at each level.

相关文章

Python logging设置和logger解析

Python logging设置和logger解析

一、logging模块讲解 1.函数:logging.basicConfig() 参数讲解: (1)level代表高于或者等于这个值时,那么我们才会记录这条日志 (2)filename代...

python解析xml文件实例分析

本文实例讲述了python解析xml文件的方法。分享给大家供大家参考。具体如下: python解析xml非常方便。在dive into python中也有讲解。 如果xml的结构如下:...

TensorFlow在MAC环境下的安装及环境搭建

TensorFlow在MAC环境下的安装及环境搭建

给大家分享一下TensorFlow在MAC系统中的安装步骤以及环境搭建的操作流程。 TensorFlow 底层的图模型结构清晰,容易改造;支持分布式训练;可视化效果好。如果做长期项目,接...

计算机二级python学习教程(1) 教大家如何学习python

计算机二级python学习教程(1) 教大家如何学习python

本来PHP还学艺不精,又报了计算机二级Python的考试,还有一个半月的时间,抓紧买了高教社的这两本书,今天正式开始学习这个语言,虽然没法和世界上最好的语言PHP相提并论,但是也值得一学...

python enumerate函数的使用方法总结

enumerate函数用于遍历序列中的元素以及它们的下标。 enumerate函数说明: enumerate()是python的内置函数 enumerate在字典上是枚举、列举的意思...