在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 SVD压缩图像的实现代码

python SVD压缩图像的实现代码

前言 利用SVD是可以对图像进行压缩的,其核心原因在于,图像的像素之间具有高度的相关性。 代码 # -*- coding: utf-8 -*- ''' author@cclplu...

python梯度下降法的简单示例

python梯度下降法的简单示例

梯度下降法的原理和公式这里不讲,就是一个直观的、易于理解的简单例子。 1.最简单的情况,样本只有一个变量,即简单的(x,y)。多变量的则可为使用体重或身高判断男女(这是假设,并不严谨),...

Python中的Socket 与 ScoketServer 通信及遇到问题解决方法

Socket有一个缓冲区,缓冲区是一个流,先进先出,发送和取出的可自定义大小的,如果取出的数据未取完缓冲区,则可能存在数据怠慢。其中【recv(1024)】表示从缓冲区里取最大为1024...

Python中内置数据类型list,tuple,dict,set的区别和用法

Python语言简洁明了,可以用较少的代码实现同样的功能。这其中Python的四个内置数据类型功不可没,他们即是list, tuple, dict, set。这里对他们进行一个简明的总结...

通过实例学习Python Excel操作

通过实例学习Python Excel操作

这篇文章主要介绍了通过实例学习Python Excel操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.python 读取Exc...