在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的路径: #!/usr/bin/python python 在linux中需要添加编码方式:以免出现中文乱码 # -*- coding: UTF-8...

python3 selenium自动化 frame表单嵌套的切换方法

python3 selenium自动化 frame表单嵌套的切换方法

在web自动化测试中,测试工程师经常会碰到frame表单嵌套结构,直接定位会报错,我们需要切换表单后才能成功定位。 我拿QQ邮箱登录来作为例子说下frame怎么切换。 qq邮箱页面按F...

春节到了 教你使用python来抢票回家

这篇文章主要介绍了春节到了 教你使用python来抢票回家,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 不知不觉,一年一度的春运抢票...

python实现键盘输入的实操方法

python实现键盘输入的实操方法

python中有指定的代码进行输入操作,所以今天就由小编来为大家介绍python怎么实现键盘输入。 第一首先打开电脑的python编辑工具。 再创建python项目。 第二然后应用sy...

Python在groupby分组后提取指定位置记录方法

Python在groupby分组后提取指定位置记录方法

在进行数据分析、数据建模时,我们首先要做的就是对数据进行处理,提取我们需要的信息。下面为大家介绍一些groupby的用法,以便能够更加方便地进行数据处理。 我们往往在使用groupby进...