删除最后3个版本文件夹 - Python

时间:2016-12-06 12:07:45

标签: python python-2.7 directory

我有一个带有一些软件版本的树,我需要保留最后3个版本。

我有这样的树文件夹:

版本:

文件夹:

' 10.7.0.290',' 10.7.0.289',' 10.7.0.281',' 10.7.0.278& #39;,' 10.7.0.275'

' 10.6.7.282',' 10.6.7.277',' 10.6.7.274',' 10.6.7.270& #39;

我想保留每个版本的最后3个文件夹,例如在这种情况下,我希望保留&strong;#10; 10.7.0.yyy&的最后3 #39;和' 10.6.7.xxx版本':

' 10.7.0.290',' 10.7.0.289',' 10.7.0.281'

' 10.6.7.282',' 10.6.7.277',' 10.6.7.274'

并删除此内容:

' 10.7.0.278',' 10.7.0.275'

' 10.6.7.270'

但是我的脚本只保留每个版本的最后3个而不是:

脚本:

import os
import re
import shutil

versions = []
folders = []
a = os.listdir('.')
a.sort(reverse=True)
sorted(a)

for fn in a:
    folders.append(fn)
    if os.path.isdir(fn):
        version = (re.match('^([0-9]+\.[0-9]+\.[0-9]+).*', fn)).group(1)
        if (version) not in versions:
            versions.append(version)

result = []


for version in versions:
    temp = []
    for folder in folders:
        if folder.startswith(version):
            temp.append(folder)

    result.append(temp)
print result    

for version in result:
   for i in range(max(len(version) - 3, 0)):
       print version[i]
 #   shutil.rmtree(version[i])

结果

10.7.0.290 #this files are deleted
10.7.0.289 #this files are deleted
10.6.7.282 #this files are deleted

3 个答案:

答案 0 :(得分:1)

让我们指出所需的步骤。

  1. 使用os.listdir获取给定路径中所有目录的列表。
  2. 对列表进行排序,并通过切片列表
  3. 获取前3个目录名称
  4. 使用shutil.rmtree在循环内删除它们。
  5. 所以,解决方案应该是:

    from os import listdir
    import shutil
    
    directories_to_delete = sorted(listdir('desired/path'))[:3]
    for directory in directories_to_delete:
        shutil.rmtree(directory)
    

答案 1 :(得分:1)

您可以将文件夹名称转换为int的列表。这样可以正确排序版本,以便10被认为晚于9。然后,您可以按相反顺序对它们进行排序,并使用groupby根据3个第一个组件对它们进行分组。然后,对于每个组,您可以使用dropwhile丢弃前3个项目并删除其余项目:

from itertools import groupby, dropwhile

FOLDERS = [
    '10.7.0.290', '10.7.0.289', '10.7.0.281', '10.7.0.278', '10.7.0.275',
    '10.6.7.282', '10.6.7.277', '10.6.7.274', '10.6.7.270'
]

components = ([int(x) for x in fn.split('.')] for fn in FOLDERS)
for _, g in groupby(sorted(components, reverse=True), key=lambda x: x[:3]):
    for _, fn in dropwhile(lambda x: x[0] < 3, enumerate(g)):
        print 'Remove {}'.format('.'.join(str(x) for x in fn))

输出:

Remove 10.7.0.278
Remove 10.7.0.275
Remove 10.6.7.270

答案 2 :(得分:0)

这样的事情会起作用:

import os
import shutil

from collections import defaultdict


versions = defaultdict(list)
directories = next(os.walk('.'))[1]
for directory in directories:
    major, minor, patch, minor_patch = directory.split('.')
    versions['{}.{}.{}'.format(major, minor, patch)].append(int(minor_patch))

for version, minor_patches in versions.items():
    minor_patches.sort(reverse=True)
    to_remove = minor_patches[3:]
    for minor_patch in to_remove:
        path = '{}.{}'.format(version, minor_patch)
        shutil.rmtree('./{}'.format(path))

示例:

[$:/tmp/folders] $ tree
.
├── 10.6.7.270
├── 10.6.7.274
├── 10.6.7.277
├── 10.6.7.282
├── 10.7.0.275
├── 10.7.0.278
├── 10.7.0.281
├── 10.7.0.289
├── 10.7.0.290
└── folders.py

9 directories, 1 file
[$:/tmp/folders] $ python folders.py 
[$:/tmp/folders] $ tree
.
├── 10.6.7.274
├── 10.6.7.277
├── 10.6.7.282
├── 10.7.0.281
├── 10.7.0.289
├── 10.7.0.290
└── folders.py

6 directories, 1 file