在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中的pass语句使用方法讲解

 Python pass语句使用当语句要求不希望任何命令或代码来执行。 pass语句是一个空(null)操作;在执行时没有任何反应。pass也是代码最终会是有用的,但暂时不用写...

Linux下python制作名片示例

Linux下python制作名片示例

建立cards_main文件: # _*_ coding:utf-8 _*_ """ file: cards_main.py date: 2018-07-18 19:47 auth...

python用post访问restful服务接口的方法

具体代码如下: import requests import json data={"ids": ["00007190","00007191"]} url="http://XXXX...

Python无损音乐搜索引擎实现代码

Python无损音乐搜索引擎实现代码

  研究了一段时间酷狗音乐的接口,完美破解了其vip音乐下载方式,想着能更好的追求开源,故写下此篇文章,本文仅供学习参考。虽然没什么技术含量,但都是自己一点一点码出来,一点一点...

pandas 选取行和列数据的方法详解

pandas 选取行和列数据的方法详解

前言 本文介绍在 pandas 中如何读取数据行列的方法。数据由行和列组成,在数据库中,一般行被称作记录 (record),列被称作字段 (field)。回顾一下我们对记录和字段的获取方...