在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.

相关文章

django中瀑布流写法实例代码

django中瀑布流写法实例代码

django中瀑布流初探 img.html <!DOCTYPE html> <html lang="en"> <head> <meta...

Python入门篇之数字

Python入门篇之数字

数字类型   数字提供了标量贮存和直接访问。它是不可更改类型,也就是说变更数字的值会生成新的对象。当然,这个过程无论对程序员还是对用户都是透明的,并不会影响软件的开发方式。 P...

Python实现打印螺旋矩阵功能的方法

Python实现打印螺旋矩阵功能的方法

本文实例讲述了Python实现打印螺旋矩阵功能的方法。分享给大家供大家参考,具体如下: 一、问题描述 输入N, 打印 N*N 螺旋矩阵 比如 N = 3,打印: 1 2 3 8 9...

Python中的上下文管理器和with语句的使用

Python2.5之后引入了上下文管理器(context manager),算是Python的黑魔法之一,它用于规定某个对象的使用范围。本文是针对于该功能的思考总结。 为什么需要上下文管...

Python使用multiprocessing实现一个最简单的分布式作业调度系统

 mutilprocess像线程一样管理进程,这个是mutilprocess的核心,他与threading很是相像,对多核CPU的利用率会比threading好的多。 介绍 P...