在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使用pylab库实现绘制直方图功能示例

Python使用pylab库实现绘制直方图功能示例

本文实例讲述了Python使用pylab库实现绘制直方图功能。分享给大家供大家参考,具体如下: Python直方图 #!/usr/bin/python # -*- coding: u...

Pytorch反向求导更新网络参数的方法

方法一:手动计算变量的梯度,然后更新梯度 import torch from torch.autograd import Variable # 定义参数 w1 = Variable(...

python批量查询、汉字去重处理CSV文件

CSV文件用记事本打开后一般为由逗号隔开的字符串,其处理方法用Python的代码如下。为方便各种程度的人阅读在代码中有非常详细的注释。 1.查询指定列,并保存到新的csv文件。 #...

python 解决动态的定义变量名,并给其赋值的方法(大数据处理)

最近消费kafka数据到磁盘的时候遇到了这样的问题: 需求:每天大概有1千万条数据,每条数据包含19个字段信息,需要将数据写到服务器磁盘,以第二个字段作为大类建立目录,第7个字段作为小类...

python石头剪刀布小游戏(三局两胜制)

Python 石头剪刀布小游戏(三局两胜),供大家参考,具体内容如下 import random all_choioces = ['石头', '剪刀', '布'] win_list...