在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删除Java源文件中全部注释的实现方法

本文实例讲述了Python删除Java源文件中全部注释的实现方法。分享给大家供大家参考,具体如下: 同事想删除一个Java项目中的全部注释,让我帮忙想想办法。 没找不到合适工具,就写了这...

Python实现的端口扫描功能示例

本文实例讲述了Python实现的端口扫描功能。分享给大家供大家参考,具体如下: 一 代码 import sys import socket import multiprocessin...

Python 过滤错误log并导出的实例

前言: 测试过程中获取App相关log后,如何快速找出crash的部分,并导出到新的文件呢? 感兴趣的话,继续往下看吧~ 思路:遍历多个日志文件,找出含有Error和Crash的日志,并...

深入理解Python装饰器

装饰器简介: 装饰器(decorator)是一种高级Python语法。装饰器可以对一个函数、方法或者类进行加工。在Python中,我们有多种方法对函数和类进行加工,比如在Python闭包...

如何基于Python创建目录文件夹

这篇文章主要介绍了如何基于Python创建目录文件夹,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Python对文件的操作还算是方便...