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

相关文章

python3.6+opencv3.4实现鼠标交互查看图片像素

python3.6+opencv3.4实现鼠标交互查看图片像素

在利用opencv进行图片处理时,经常需要查看图片关心区域或位置的像素数值,苦于没有应手的小软件,我用python3.6+opencv3.4简单编制一个小工具,供大家使用。 流程 1...

Python中列表list以及list与数组array的相互转换实现方法

本文实例讲述了Python中list以及list与array的相互转换实现方法。分享给大家供大家参考,具体如下: python中的list是一种有序集合,可以随时增删元素; # -*...

python的三目运算符和not in运算符使用示例

python的三目运算符和not in运算符使用示例

三目运算符也就是三元运算符 一些语言(如Java)的三元表达式形如: 判定条件?为真时的结果:为假时的结果 result=x if x Python的三元表达式有如下几种书写方法...

PyQt5每天必学之创建窗口居中效果

PyQt5每天必学之创建窗口居中效果

本文实例为大家分享了PyQt5如何能够创建在桌面屏幕上居中窗口的具体代码,供大家参考,具体内容如下 下面的脚本说明我们如何能够创建在桌面屏幕上居中的窗口。 #!/usr/bin/py...

python批量导入数据进Elasticsearch的实例

ES在之前的博客已有介绍,提供很多接口,本文介绍如何使用python批量导入。ES官网上有较多说明文档,仔细研究并结合搜索引擎应该不难使用。 先给代码 #coding=utf-8 f...