在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 20行简单实现有道在线翻译的详解

Python 20行简单实现有道在线翻译的详解

简介 主要是尝试简单的使用pyhton的爬虫功能,于是使用有道进行尝试,并没有进行深入的诸如相关api的调用。 以下是需要的POST数据 代码 以下是相关部分的代码: import...

python ChainMap的使用和说明详解

ChainMap是Python collections模块中的一个成员,它用来将多个map组合到一起。Chain是链条的意思,字面意思就是把map串联到一起。 问题的背景是我们有多个字典...

Python警察与小偷的实现之一客户端与服务端通信实例

本文实例讲述了Python警察与小偷的实现之一客户端与服务端通信,分享给大家供大家参考。具体方法分析如下: 该实例来源于ISCC 2012 破解关第四题 目的是通过逆向police,实...

python修改字典内key对应值的方法

本文实例讲述了python修改字典内key对应值的方法。分享给大家供大家参考。具体实现方法如下: d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # ma...

详解numpy.meshgrid()方法使用

详解numpy.meshgrid()方法使用

一句话解释numpy.meshgrid()——生成网格点坐标矩阵。 关键词:网格点,坐标矩阵 网格点是什么?坐标矩阵又是什么鬼? 看个图就明白了: 图中,每个交叉点都是网格点,描...