在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编程之黑板上排列组合,你舍得解开吗

考虑这样一个问题,给定一个矩阵(多维数组,numpy.ndarray()),如何shuffle这个矩阵(也就是对其行进行全排列),如何随机地选择其中的k行,这叫组合,实现一种某一维度空间...

python实现监控某个服务 服务崩溃即发送邮件报告

前言:最近我们的升级服务器有点不太稳定,经常崩溃掉。然后客户连接不上,跟我们反馈才知道。所以写这个脚本的目的就是为了比客户提前知道升级服务的运行状况,一旦崩溃掉,就能第一时间登录上去,开...

pandas使用apply多列生成一列数据的实例

如下所示: import pandas as pd def my_min(a, b): return min(abs(a),abs(b)) s = pd.Series([...

python套接字流重定向实例汇总

将套接字流重定向到标准输入或输出流 #!/usr/bin/env python3 """ 测试socket-stream 重定向模式 """ import sys,os,time f...

对Python进行数据分析_关于Package的安装问题

对Python进行数据分析_关于Package的安装问题

一、为什么要使用Python进行数据分析? python拥有一个巨大的活跃的科学计算社区,拥有不断改良的库,能够轻松的集成C,C++,Fortran代码(Cython项目),可以同时用于...