在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和JavaScript间代码转换的4个工具

Python和JavaScript间代码转换的4个工具

选 Python 还是 JavaScript?虽然不少朋友还在争论二者目前谁更强势、谁又拥有着更为光明的发展前景,但毫无疑问,二者的竞争在 Web 前端领域已经拥有明确的答案。立足于浏览...

使用PDB模式调试Python程序介绍

以前在windows下一直用的idel带的功能调试python程序,在linux下没调试过。(很多时候只是print)就从网上查找一下~ 方法: 复制代码 代码如下: python -m...

python的构建工具setup.py的方法使用示例

python的构建工具setup.py的方法使用示例

本文介绍了python的构建工具setup.py,分享个大家,具体如下: 一、构建工具setup.py的应用场景 在安装python的相关模块和库时,我们一般使用“pip install...

Python中的map、reduce和filter浅析

1、先看看什么是 iterable 对象 以内置的max函数为例子,查看其doc:复制代码 代码如下:>>> print max.__doc__max(iterable...

Python 图像对比度增强的几种方法(小结)

Python 图像对比度增强的几种方法(小结)

图像处理工具——灰度直方图 灰度直方图时图像灰度级的函数,用来描述每个灰度级在图像矩阵中的像素个数或者占有率。 例子:矩阵 图片来自网络,侵删! 上面图片的灰度直方图 p...