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

相关文章

Django REST framework 分页的实现代码

官方文档[这里] 用于分页的模块: Pagination Django REST framework 有内置 Pagination 模块,无需额外安装, 只需做简单的配置. 配置什么呢&...

Python中生成器和迭代器的区别详解

Python中生成器和迭代器的区别(代码在Python3.5下测试): Num01–>迭代器 定义: 对于list、string、tuple、dict等这些容器对象,使用for循环...

对sklearn的使用之数据集的拆分与训练详解(python3.6)

研修课上讲了两个例子,融合一下。 主要演示大致的过程: 导入->拆分->训练->模型报告 以及几个重要问题: ①标签二值化 ②网格搜索法调参 ③k折交叉验证 ④增加...

解决python xx.py文件点击完之后一闪而过的问题

解决python xx.py文件点击完之后一闪而过的问题

1.问题复现: 有时候我们去点击.py文件 文件里明明有打印信息,却一闪而过,没有任何显示 比如以下内容 #!/usr/local/bin/python import sys pri...

python中根据字符串调用函数的实现方法

在python中可以根据字符串来调用函数: 1、使用getattr从字符串来调用函数 在多进程中,可能传递过来的是一个字符串,那么我怎么来调用一个已经存在的函数呢,主要就是使用到geta...