在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.

相关文章

Python3 单行多行万能正则匹配方法

可匹配单行,也支持换行匹配 [\s\S]*? 加上括号,效果更好 ([\s\S]*?) 以上这篇Python3 单行多行万能正则匹配方法就是小编分享给大家的全部内容了,希...

Python程序员面试题 你必须提前准备!(答案及解析)

Python程序员面试题 你必须提前准备!(答案及解析)

在发布《Python程序员面试,这些问题你必须提前准备!》一文后,应广大程序员朋友的强烈要求,小编就Python程序员面试必备问题整理了一份参考答案,希望能对准备换工作的程序员朋友有所帮...

python绘图库Matplotlib的安装

python绘图库Matplotlib的安装

本文简单介绍了Python绘图库Matplotlib的安装,简介如下: matplotlib是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地...

Python实现的删除重复文件或图片功能示例【去重】

Python实现的删除重复文件或图片功能示例【去重】

本文实例讲述了Python实现的删除重复文件或图片功能。分享给大家供大家参考,具体如下: 通过python爬虫或其他方式保存的图片文件通常包含一些重复的图片或文件, 通过下面的pytho...

python数据结构之图深度优先和广度优先实例详解

本文实例讲述了python数据结构之图深度优先和广度优先用法。分享给大家供大家参考。具体如下: 首先有一个概念:回溯   回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达...