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

相关文章

wxPython电子表格功能wx.grid实例教程

本文实例为大家分享了wxPython电子表格功能的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python #encoding: utf8 import wx...

使用 Python 实现文件递归遍历的三种方式

今天有个脚本需要遍历获取某指定文件夹下面的所有文件,我记得很早前也实现过文件遍历和目录遍历的功能,于是找来看一看,嘿,不看不知道,看了吓一跳,原来之前我竟然用了这么搓的实现。 先发出来看...

Django框架下静态模板的继承操作示例

本文实例讲述了Django框架下静态模板的继承操作。分享给大家供大家参考,具体如下: 前言:第一篇博客,毕业校招在即,抽空把做过的项目都整理一下。 开发环境:python3.4,djan...

python编码总结(编码类型、格式、转码)

python编码总结(编码类型、格式、转码)

本文详细总结了python编码。分享给大家供大家参考,具体如下: 【所谓unicode】 unicode是一种类似于符号集的抽象编码,它只规定了符号的二进制代码,却没有规定这个二进制代码...

Python 利用切片从列表中取出一部分使用的方法

我想从列表中取出一部分拿来使用,可以创建切片,指定需要使用的第一个元素和最后一个元素的索引 使用例子,说明切片的使用 #创建一个数字列表,代表我有100页文章,然后进行分页显示 ma...