在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开启摄像头,我手上只有两个uvc免驱的摄像头,性能一般。利用python开启摄像头费了一番功夫,主要原因是我的摄像头都不能用cv2的VideCa...

Python实现文件信息进行合并实例代码

Python实现文件信息进行合并实例代码

将电话簿TeleAddressBook.txt和电子邮件EmailAddressBook.txt合并为一个完整的AddressBook.txt def main(): ftele...

Python中str.join()简单用法示例

本文实例讲述了Python中str.join()简单用法。分享给大家供大家参考,具体如下: Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。其中,序...

图文详解WinPE下安装Python

图文详解WinPE下安装Python

本文介绍了WinPE下安装Python的具体步骤,供大家参考,具体内容如下 一、下载Python Windows安装包,最新版本为3.3.0 下载地址:http://www.python...

Python Web编程之WSGI协议简介

本文实例讲述了Python Web编程之WSGI协议。分享给大家供大家参考,具体如下: WSGI简介 Web框架和Wen服务器之间需要进行通信,如果在设计时它们之间无法相互匹配,那么对框...