在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 Unittest根据不同测试环境跳过用例的方法

Python Unittest根据不同测试环境跳过用例的方法

前言 在利用单元测试框架执行测试用例的过程中,有时只需要执行一部分用例,或者跳过某些暂不需要执行的用例,python的unittest框架就内置这样的功能。 本文章会讲述以下几个内容:...

scrapy自定义pipeline类实现将采集数据保存到mongodb的方法

本文实例讲述了scrapy自定义pipeline类实现将采集数据保存到mongodb的方法。分享给大家供大家参考。具体如下: # Standard Python library im...

Python正则表达式使用经典实例

下面列出Python正则表达式的几种匹配用法,具体内容如下所示: 此外,关于正则的一切http://deerchao.net/tutorials/regex/regex.htm 1....

Python列表常见操作详解(获取,增加,删除,修改,排序等)

本文实例讲述了Python列表常见操作。分享给大家供大家参考,具体如下: 列表是由一系列按特定顺序排列的元素组成的对象。因为列表通常包含多个元素, 所以建议给列表指定一个表示复数的名称。...

详解TensorFlow在windows上安装与简单示例

详解TensorFlow在windows上安装与简单示例

本文介绍了详解TensorFlow在windows上安装与简单示例,分享给大家,具体如下: 安装说明 平台:目前可在Ubuntu、Mac OS、Windows上安装 版本:提供gpu...