在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中的延迟绑定原理详解

直接看下面例子 my_ld = [lambda x:x*i for i in range(3)] my_list = [ld(2) for ld in my_ld] print(my...

Python从list类型、range()序列简单认识类(class)【可迭代】

本文实例讲述了Python从list类型、range()序列简单认识类(class)。分享给大家供大家参考,具体如下: list类型 定义: items = [] 这就定义了一...

numpy.array 操作使用简单总结

import numpy as np numpy.array 常用变量及参数 dtype变量,用来存放数据类型, 创建数组时可以同时指定。 shape变量, 存放数组的大...

python修改注册表终止360进程实例

本文实例讲述了python修改注册表终止360进程的实现方法。分享给大家供大家参考。 具体实现代码如下: import _winreg import os import shutil...

请不要重复犯我在学习Python和Linux系统上的错误

请不要重复犯我在学习Python和Linux系统上的错误

本人已经在运维行业工作了将近十年,我最早接触Linux是在大二的样子,那时候只追求易懂,所以就选择了Ubuntu作为学习、使用的对象,它简单、易用、好操作、界面绚丽,对于想接触Linux...