在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正则表达式面试题解答

三道python正则表达式面试题,具体如下 1.去除以下html文件中的标签,只显示文本信息。 <div> <p>岗位职责:</p> <p&...

解决django后台管理界面添加中文内容乱码问题

在学习使用django做一个简单的个人博客项目,通过admin后台添加中文文章内容的时候,遇到中文内容显示乱码的问题。 排除了网上资料中的提到的几个问题: 1.数据上传默认采用的是un...

pandas表连接 索引上的合并方法

如下所示: left1 = pd.DataFrame({‘key':[‘a','b','a','a','b','c'],'value':range(6)}) right1 = pd...

Python实现的个人所得税计算器示例

本文实例讲述了Python实现的个人所得税计算器。分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- """ Created on Sat Apr 15...

Python进度条实时显示处理进度的示例代码

前言 在大多数时候,我们的程序会一直进行循环处理。这时候,我们非常希望能够知道程序的处理进度,由此来决定接下来该做些什么。接下来告诉大家如何简单又漂亮的实现这一功能。 如何使用这个类 使...