在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正则表达式re模块详细介绍

本模块提供了和Perl里的正则表达式类似的功能,不关是正则表达式本身还是被搜索的字符串,都可以是Unicode字符,这点不用担心,python会处理地和Ascii字符一样漂亮。 正则表...

Python实现输入二叉树的先序和中序遍历,再输出后序遍历操作示例

本文实例讲述了Python实现输入二叉树的先序和中序遍历,再输出后序遍历操作。分享给大家供大家参考,具体如下: 实现一个功能:     输入:一颗二叉树的先...

Python 导入文件过程图解

Python 导入文件过程图解

1、同级目录下调用 若在程序 testone.py 中导入模块 testtwo.py , 则直接使用 【import testtwo 或 from testtwo import *】...

浅谈django url请求与数据库连接池的共享问题

浅谈django url请求与数据库连接池的共享问题

但凡介绍数据库连接池的文章,都会说“数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现得尤为突出。对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响...

Pycharm远程调试原理及具体配置详解

Pycharm远程调试原理及具体配置详解

前言 工作中使用Pycharm作为python开发的IDE,作为专业的python集成开发环境,其功能之强大令人折服。开发过程中Debug是必不可少的。平时经常使用Pycharm的rem...