在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.

相关文章

PyQt5重写QComboBox的鼠标点击事件方法

最近学PyQt5,想要做一个串口调试助手来练练手,之前用了正点原子的串口上位机,觉得点击ComboBox自动检测串口这个功能很棒,之前用QT5写串口调试助手的时候也想加入这个功能,但是一...

在numpy矩阵中令小于0的元素改为0的实例

如下所示: >>> import numpy as np >>> a = np.random.randint(-5, 5, (5, 5)) >...

python控制台中实现进度条功能

python控制台中实现进度条功能

我们大多数人都希望写一些简单的python脚本的同时都想能够在程序运行的过程中实现进度条的功能以便查看程序运行的速度或者进度。今天就和大家探讨这个问题:如何在python控制台中实现进度...

Python减少循环层次和缩进的技巧分析

Python减少循环层次和缩进的技巧分析

本文实例分析了Python减少循环层次和缩进的技巧。分享给大家供大家参考,具体如下: 我们知道Python中冒号和缩进代表大括号,这样写已经可以节省很多代码行数,但是可以更优化,尽可能减...

深入理解Javascript中的this关键字

自从接触javascript以来,对this参数的理解一直是模棱两可。虽有过深入去理解,但却也总感觉是那种浮于表面,没有完全理清头绪。 但对于this参数,确实会让人产生很多误解。那么t...