在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求解平方根的方法

本文实例讲述了Python求解平方根的方法。分享给大家供大家参考。具体如下: 主要通过SICP的内容改写而来。基于newton method求解平方根。代码如下: #!/usr/bi...

python双端队列原理、实现与使用方法分析

本文实例讲述了python双端队列原理、实现与使用方法。分享给大家供大家参考,具体如下: 双端队列 双端队列(deque,全名double-ended queue),是一种具有队列和栈的...

python中join()方法介绍

描述 Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。 语法 join()方法语法: str . join ( sequence ) 参数 sequ...

Python实现的快速排序算法详解

本文实例讲述了Python实现的快速排序算法。分享给大家供大家参考,具体如下: 快速排序基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有...

python实现斐波那契数列的方法示例

python实现斐波那契数列的方法示例

介绍 斐波那契数列,又称黄金分割数列,指的是这样一个数列:0、1、1、2、3、5、8、13、21、……在数学上,斐波纳契数列以如下递归的方法定义: F(0)=0,F(1)=1,F(n)=...