在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+webdriver自动化环境搭建步骤详解

python+webdriver自动化环境搭建步骤详解

python是一个很好脚本语言工具,现在也比较流行的一个脚本语言工具,对目前web自动化可以用的比较是webdriver框架进行自动化测试,脚本写起来较简单,运行的占用的内容较小。那么对...

Python格式化输出字符串方法小结【%与format】

python格式化字符串有%和{}两种 字符串格式控制符. 字符串输入数据格式类型(%格式操作符号) %% 百分号标记#就是输出一个%...

Python partial函数原理及用法解析

这篇文章主要介绍了Python partial函数原理及用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 介绍 partial...

python 列表递归求和、计数、求最大元素的实例

利用python的递归来执行求和、计数、求最大元素的方法简直溜到爆,这里粘贴一下代码: 列表的递归求和: def sum(list): if list==[]: return...

Python扫描IP段查看指定端口是否开放的方法

本文实例讲述了Python扫描IP段查看指定端口是否开放的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/local/bin/python #-*- coding: U...