在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中Numpy mat的使用详解

前面介绍过用dnarray来模拟,但mat更符合矩阵,这里的mat与Matlab中的很相似。(mat与matrix等同) 基本操作 >>> m= np.mat([1...

python3 selenium自动化测试 强大的CSS定位方法

ccs的优点:css相对xpath语法比xpath简洁,定位速度比xpath快 css的缺点:css不支持用逻辑运算符来定位,而xpath支持。css定位语法形式多样,相对xpath比较...

Python使用re模块正则提取字符串中括号内的内容示例

本文实例讲述了Python使用re模块正则提取字符串中括号内的内容操作。分享给大家供大家参考,具体如下: 直接上代码吧: # -*- coding:utf-8 -*- #! pyth...

Python中的ctime()方法使用教程

 ctime()方法转换,因为历元到表示本地时间的字符串表示以秒为单位的时间。如果不设置秒时或None,所返回的时间的当前time()被使用。使用asctime(localti...