在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批量下载图片的三种方法

有三种方法,一是用微软提供的扩展库win32com来操作IE,二是用selenium的webdriver,三是用python自带的HTMLParser解析。win32com可以获得类似j...

django 环境变量配置过程详解

django 环境变量配置过程详解

刚开始使用django,在创建第一个app时被提示不知道命令runserver,百度得出是环境变量的问题。 1、配置python变量环境,C:\Python27\;C:\Python27...

python ddt实现数据驱动

ddt 是第三方模块,需安装, pip install ddt DDT包含类的装饰器ddt和两个方法装饰器data(直接输入测试数据) 通常情况下,data中的数据按照一个参数传递给测试...

python pygame实现方向键控制小球

python pygame实现方向键控制小球

最后一个项目用到了pygame,  实现方向键控制小球,对于模块不熟悉的我还是查询了一些资料介绍。 import sys import pygame from pygame...

python实现一个简单的并查集的示例代码

并查集是一种树型的数据结构,用于处理一些不相交集合的合并及查询问题。常常在使用中以森林来表示。 并查集有三种基本操作,获得根节点,判断两节点是否连通,以及将两不连通的节点相连(相当于将两...