在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实现栈的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/env python #定义一个列表来模拟栈 stack = [] #进...

python 数据清洗之数据合并、转换、过滤、排序

python 数据清洗之数据合并、转换、过滤、排序

前面我们用pandas做了一些基本的操作,接下来进一步了解数据的操作, 数据清洗一直是数据分析中极为重要的一个环节。 数据合并 在pandas中可以通过merge对数据进行合并操作。...

Django实现跨域的2种方法

jsonp 方式一:指定返回方法 # 后端 def view(request): callback = request.GET.get('callback') return...

在PyTorch中Tensor的查找和筛选例子

本文源码基于版本1.0,交互界面基于0.4.1 import torch 按照指定轴上的坐标进行过滤 index_select() 沿着某tensor的一个轴dim筛选若干个坐标 &...

python3字符串操作总结

介绍Python常见的字符串处理方式 字符串截取 >>>s = 'hello' >>>s[0:3] 'he' >>>s[:...