在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列表append和+的区别浅析

在python中使用列表的时候大家经常会需要向一个列表中添加一个元素,像下面这两种使用方法需要注意: 复制代码 代码如下: t = [1, 2, 3] t1 = t.append([4]...

Python排序搜索基本算法之冒泡排序实例分析

Python排序搜索基本算法之冒泡排序实例分析

本文实例讲述了Python排序搜索基本算法之冒泡排序。分享给大家供大家参考,具体如下: 冒泡排序和选择排序类似,也是第n次把最小的元素排在第n的位置上,也是该元素的绝对位置,只是冒泡排序...

实例讲解Python3中abs()函数

Python3 abs() 函数 描述 abs() 函数返回数字的绝对值。 语法 以下是 abs() 方法的语法: abs( x ) 参数 x-- 数值表达式,可以是整数,浮点...

解决python删除文件的权限错误问题

使用os.remove删除文件,总是遇到错误:PermissionError: WinError 找了很久没找到什么原因,以为是windows系统的问题,最后发现是删除了一个没有关闭的文...

深入理解Javascript中的this关键字

自从接触javascript以来,对this参数的理解一直是模棱两可。虽有过深入去理解,但却也总感觉是那种浮于表面,没有完全理清头绪。 但对于this参数,确实会让人产生很多误解。那么t...