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

yipeiwu_com5年前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模块学习笔记

模块级函数 logging.getLogger([name]):返回一个logger对象,如果没有指定名字将返回root loggerlogging.debug()、logging.in...

对python 中re.sub,replace(),strip()的区别详解

对python 中re.sub,replace(),strip()的区别详解

1.strip(): str.strip([chars]);去除字符串前面和后面的所有设置的字符串,默认为空格 chars -- 移除字符串头尾指定的字符序列。 st = " he...

Python jieba库用法及实例解析

Python jieba库用法及实例解析

1、jieba库基本介绍 (1)、jieba库概述 jieba是优秀的中文分词第三方库 - 中文文本需要通过分词获得单个的词语 - jieba是优秀的中文分词第三方库,需要额...

python自动发送测试报告邮件功能的实现

python自动发送测试报告邮件功能的实现

自动化发邮件功能也是自动化测试项目中的重要需求之一。在自动化脚本运行完成之后,邮箱就可以收到最新的测试报告结果,把这种主动的且不及时的查看变成被动且及时的查收,就方便多了。 首先我们需要...

Python分治法定义与应用实例详解

本文实例讲述了Python分治法定义与应用。分享给大家供大家参考,具体如下: 分治法所能解决的问题一般具有以下几个特征: 1) 该问题的规模缩小到一定的程度就可以容易地解决 2) 该问题...