在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中如何正确使用正则表达式的详细模式(Verbose mode expression)

python中如何正确使用正则表达式的详细模式(Verbose mode expression)

简单介绍 正则表达式并不是Python的一部分。正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十分强大。得益于这一...

PyQt5打开文件对话框QFileDialog实例代码

本文研究的主要是PyQt5打开文件对话框QFileDialog的代码示例,具体如下。 单个文件打开 QFileDialog.getOpenFileName() 多个文件打开 QFile...

Python实现读取及写入csv文件的方法示例

Python实现读取及写入csv文件的方法示例

本文实例讲述了Python实现读取及写入csv文件的方法。分享给大家供大家参考,具体如下: 新建csvData.csv文件,数据如下: 具体代码如下: # coding:utf-8...

python matplotlib折线图样式实现过程

python matplotlib折线图样式实现过程

这篇文章主要介绍了python matplotlib折线图样式实现过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一:简单的折线图...

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

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