在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和C语言混编的几种方式(推荐)

Python这些年风头一直很盛,占据了很多领域的位置,Web、大数据、人工智能、运维均有它的身影,甚至图形界面做的也很顺,乃至full-stack这个词语刚出来的时候,似乎就是为了描述它...

Python中six模块基础用法

背景 大家知道现在python主要有两个大的版本,一个是python2另一个是python3,那么不同的人可能会习惯不同的版本,而python2和python3又有一些区别和不兼容的地...

Python实现随机选择元素功能

Python实现随机选择元素功能

如果要从序列中随机挑选元素,我们可以使用random模块的random.choice()方法: 如果想要取出N个元素,将选出的元素一处以做进一步的考察,可以使用random.sampl...

python使用socket向客户端发送数据的方法

本文实例讲述了python使用socket向客户端发送数据的方法。分享给大家供大家参考。具体如下: import socket, sys port = 55555 host = 'l...

Python 执行字符串表达式函数(eval exec execfile)

仔细研读后学习了三个函数: eval:计算字符串中的表达式 exec:执行字符串中的语句 execfile:用来执行一个文件 需注意的是,exec是一个语句,而eval()和execfi...