在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导包的几种方法(自定义包的生成以及导入详解)

python是一门灵活的语言,也可以说python是一门胶水语言,顾名思义,就是其可以导入各类的包,python的包可以说是所有语言中最多的。当然导入包大部分是为了更快捷,更方便,效率更...

python调用windows api锁定计算机示例

调用Windows API锁定计算机 本来想用Python32直接调用,可是没有发现Python32有Windows API LockWorkStation(); 因此,就直接调用W...

python 求1-100之间的奇数或者偶数之和的实例

如下所示: i=0 sum1=0 sum2=0 while i<=100: if i%2==0: sum1+=i else: sum2+=i i+=...

Python发送邮件的实例代码讲解

一、邮件发送示例 邮件发送示例 flask_email及smtplib原生邮件发送示例,适用于基于Flask框架开发,但是内部设置的定时任务发送邮件/或提供离线接口发送邮件操作 1....

网易有道2017内推编程题 洗牌(python)

本文实例为大家分享了网易有道2017内推编程题:洗牌,供大家参考,具体内容如下 ''' [编程题] 洗牌 时间限制:1秒 空间限制:32768K 洗牌在生活中十分常见,现在需要写一个程...