在Python中移动目录结构的方法

yipeiwu_com5年前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测试网络连通性示例【基于ping】

Python测试网络连通性示例【基于ping】

本文实例讲述了Python测试网络连通性。分享给大家供大家参考,具体如下: Python代码 #!/usr/bin/python # -*- coding:GBK -*- """Do...

python解释器spython使用及原理解析

python解释器spython使用及原理解析

简介 出于个人爱好和某种需求,我再16年对python的解释器产生了浓厚兴趣,并且下定决心重新实现一个版本。我个人再游戏服务器开发中,对c++嵌入lua和python都有着丰富应用经验...

python判断给定的字符串是否是有效日期的方法

本文实例讲述了python判断给定的字符串是否是有效日期的方法。分享给大家供大家参考。具体分析如下: 这里python判断给定的字符串是否是一个有效的日期,如果是一个日期格式的字符串,该...

python实现冒泡排序算法的两种方法

什么是冒泡排序? 冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法。 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列...

python实现二维数组的对角线遍历

python实现二维数组的对角线遍历

本文实例为大家分享了python实现二维数组的对角线遍历,供大家参考,具体内容如下 第一种情况:从左上角出发,右下角结束 要完成的事情,就像下图: 话不多说,直接上Python实现代码...