在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设计模式之外观模式。分享给大家供大家参考,具体如下: 外观模式(Facade Pattern):为子系统中的一组接口提供一个一致界面,此模式定义一个高层接口,使...

Django在win10下的安装并创建工程

Django在win10下的安装并创建工程

Django的核心(1.4+)可以运行在从2.5到2.7之间的任何Python版本。 我的电脑是操作系统是window10 ,内存是4G。 1。下载django 官网地址:https...

一个超级简单的python web程序

在MAC/LINUX环境下,执行vi hello.py命令,并输入以下代码 import web import sys urls = ("/Service/hello","hel...

Python Matplotlib实现三维数据的散点图绘制

Python Matplotlib实现三维数据的散点图绘制

一、背景   近期项目即将开展,计划第一步就是实现数据的可视化,所以先学习一下数据展示相关Demo。选用Python2.7与Matplotlib来实现,平台采用Pycharm,值得一提的...

基于Python+Appium实现京东双十一自动领金币功能

基于Python+Appium实现京东双十一自动领金币功能

背景:做任务领金币的过程很无聊,而且每天都是重复同样的工作,非常符合自动化的定义; 工具:python,appium,Android 手机(我使用的是安卓6.0的),数据线一根; 开搞前...