在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实现匹配163邮箱的代码: #-*- coding:utf-8 -*- __author__ = '杨鑫' import re text = in...

pycharm修改文件的默认打开方式的步骤

pycharm修改文件的默认打开方式的步骤

有时我们用pycharm打开某个文件的时候,默认的打开方式是不正确的,那么如何设置呢?下面小编给大家分享一下。 首先我们点击File菜单,然后选择Setting,如下图所示 接着找到E...

python小项目之五子棋游戏

python小项目之五子棋游戏

本文实例为大家分享了python五子棋游戏的具体代码,供大家参考,具体内容如下 1.项目简介 在刚刚学习完python套接字的时候做的一个五子棋小游戏,可以在局域网内双人对战,也可以和电...

Python中json格式数据的编码与解码方法详解

Python中json格式数据的编码与解码方法详解

本文实例讲述了Python中json格式数据的编码与解码方法。分享给大家供大家参考,具体如下: python从2.6版本开始内置了json数据格式的处理方法。 1、json格式数据编码...

python使用PythonMagick将jpg图片转换成ico图片的方法

本文实例讲述了python使用PythonMagick将jpg图片转换成ico图片的方法。分享给大家供大家参考。具体分析如下: 这里使用到了PythonMagick模块,关于Python...