在Python中移动目录结构的方法

yipeiwu_com5年前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.

相关文章

Python3读取文件常用方法实例分析

本文实例讲述了Python3读取文件常用方法。分享给大家供大家参考。具体如下: ''''' Created on Dec 17, 2012 读取文件 @author: liur...

Python魔术方法详解

Python魔术方法详解

介绍 此教程为我的数篇文章中的一个重点。主题是魔术方法。 什么是魔术方法?他们是面向对象的Python的一切。他们是可以给你的类增加"magic"的特殊方法。他们总是被双下划线所...

使用go和python递归删除.ds store文件的方法

python版本:复制代码 代码如下:#!/usr/bin/env pythonimport os, sys;def walk(path):  print "cd directory:"...

详解PyTorch中Tensor的高阶操作

详解PyTorch中Tensor的高阶操作

条件选取:torch.where(condition, x, y) → Tensor 返回从 x 或 y 中选择元素的张量,取决于 condition 操作定义: 举个例子:...

django 单表操作实例详解

前面视图层,模板层、路由层都写了大概,项目肯定是会和数据库打交道,那就讲讲orm的单表查询吧,直接写过一点点,不太全面。 1、项目刚创建好,我们需要在settings里配置一下(用my...