在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 根据数据模板创建shapefile的实现

废话不多说,我就直接上代码让大家看看吧! #!/usr/bin/env python # -*- coding: utf-8 -*- # @File : copyShapefile....

python使用循环打印所有三位数水仙花数的实例

首先水仙花数是什么? 水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、...

python 计算一个字符串中所有数字的和实例

如下所示: # 计算一个字符串中所有数字的和 def numsum(s): sum = 0 #定义变量,准备记录数字的和 for...

Python生成随机验证码的两种方法

使用python生成随机验证码的方法有很多种,今天小编给大家分享两种方法,大家可以灵活运用这两种方法,设计出适合自己的验证码方法。 方法一: 利用range方法,对于range方法不清楚...

对python中list的拷贝与numpy的array的拷贝详解

对python中list的拷贝与numpy的array的拷贝详解

1.python中列表list的拷贝,会有什么需要注意的呢? python变量名相当于标签名。 list2=list1 ,直接赋值,实质上指向的是同一个内存值。任意一个变量list1...