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

相关文章

使用IDLE的Python shell窗口实例详解

使用IDLE的Python shell窗口实例详解

启动IDLE后会打开Python shell窗口。当键入代码 时,它会基于Python语法提供自动缩进和代码着色功能。 使用IDLE中的Python shell。代码在输入时会自动着...

Python 专题六 局部变量、全局变量global、导入模块变量

Python 专题六 局部变量、全局变量global、导入模块变量

定义在函数内的变量有局部作用域,在一个模块中最高级别的变量有全局作用域。本文主要讲述全局变量、局部变量和导入模块变量的方法。 参考:《Python核心编程 (第二版)》 一. 局部变量...

用Python实现通过哈希算法检测图片重复的教程

用Python实现通过哈希算法检测图片重复的教程

Iconfinder 是一个图标搜索引擎,为设计师、开发者和其他创意工作者提供精美图标,目前托管超过 34 万枚图标,是全球最大的付费图标库。用户也可以在 Iconfinder 的交易板...

用Python识别人脸,人种等各种信息

用Python识别人脸,人种等各种信息

最近几天了解了一下人脸识别,应用场景可以是图片标注,商品图和广告图中有没有模特,有几个模特,模特的性别,年龄,颜值,表情等数据的挖掘。 基础的识别用dlib来实现,dlib是一个机器学...

Python3.4解释器用法简单示例

本文实例讲述了Python3.4解释器用法。分享给大家供大家参考,具体如下: Linux/Unix的系统上,Python解释器通常被安装在 /usr/local/bin/python3....