Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2014-03-13代码游戏 #27

Open
rayhe1987 opened this issue Mar 10, 2014 · 3 comments
Open

2014-03-13代码游戏 #27

rayhe1987 opened this issue Mar 10, 2014 · 3 comments

Comments

@rayhe1987
Copy link
Collaborator

分布式文件存储系统SDFS以如下的目录结构来存储文件:
qatest@qa11:~/hejl/tree-1.6.0$ ./tree /srv/qa11_nos_data/10
/srv/qa11_nos_data/10
|-- 300
| -- 0 |-- 0
| |-- 0
| | |-- 1
| | |-- 10.trash
| | |-- 1001
| | |-- 1002
| | |-- 1003
| | |-- 1004
| | |-- 1005
| | |-- 1006
| | |-- 1007
| | |-- 1008
| | |-- 1009
| | |-- 1010
| | |-- 1011.trash
| | |-- 1012
| | |-- 1013
| | |-- 1014
| | |-- 1015
| | |-- 1016
| | |-- 1017
| | |-- 1018
| | |-- 1019
| | |-- 1020
| | |-- 1021
| | |-- 1022
| | |-- 1023
| | |-- 11
| | |-- 12
| | |-- 13
| | |-- 14
| | |-- 15
| | |-- 16
| | |-- 17
| | |-- 18
| | |-- 19
| | |-- 2
| | |-- 20
| | |-- 21
| | |-- 22
| | |-- 23
| | |-- 24
| | |-- 25
| | |-- 26
| | |-- 27
| | |-- 3
| | |-- 4
| | |-- 5
| | |-- 501
| | |-- 502
| | |-- 503
| | |-- 504
| | |-- 505
| | |-- 506
| | |-- 507
| | |-- 508
| | |-- 509
| | |-- 510
| | |-- 511
| | |-- 512
| | |-- 513
| | |-- 514
| | |-- 515
| | |-- 516
| | |-- 517
| | |-- 518
| | |-- 519
| | |-- 520
| | |-- 521
| | |-- 522
| | |-- 523
| | |-- 524
| | |-- 525
| | |-- 526
| | |-- 6
| | |-- 7
| | |-- 8
| | -- 9 |-- 1
| |-- 0
| |-- 1
| |-- 1000
| |-- 1001
| |-- 1002
| |-- 2
| |-- 477
| |-- 478
| |-- 479
| |-- 480
| |-- 481
| |-- 482
| |-- 483
| |-- 484
| |-- 485
| |-- 486
| |-- 487
| |-- 488
| |-- 489
| |-- 490
| |-- 491
| |-- 492
| |-- 493
| |-- 494
| |-- 495
| |-- 496
| |-- 497
| |-- 498
| |-- 499
| |-- 500
| |-- 501
| |-- 502
| |-- 977
| |-- 978
| |-- 979
| |-- 980
| |-- 981
| |-- 982
| |-- 983
| |-- 984
| |-- 985
| |-- 986
| |-- 987
| |-- 988
| |-- 989.trash
| |-- 990
| |-- 991
| |-- 992
| |-- 993
| |-- 994
| |-- 995
| |-- 996
| |-- 997
| |-- 998.trash
| -- 999 -- 301
`-- 0
出于性能方面的考虑,用户删除一个文件, SDFS采用的是标记删除(给这个文件加上trash后缀),然后通过一个可以删除trash文件的接口统一定期物理删除trash文件,为了防止误删除,该接口只能清理20天前被删除的文件。
qa测试环境磁盘空间有限,且测试过程会产生大量数据,等不到20天磁盘空间就会被占满。因此,需要写一个小程序来统一删除带trash后缀的文件,并统计被删除文件的个数和释放的磁盘空间。题目要求如下:
1、使用python编程
2、用户可以指定需要清理的目录
3、清理完后打印出被删除文件的个数和释放的磁盘空间

@chenkan
Copy link
Owner

chenkan commented Mar 13, 2014

# coding=utf-8

import os

__author__ = 'chenkan'


def delete_trash_files(folder):
    total_release_space = 0
    total_release_files = 0

    for root, dirs, files in os.walk(folder):
        for f in files:
            if f.endswith(".trash"):
                fp = os.path.join(root, f)
                fs = os.path.getsize(fp) / 1024
                print fp + " - " + str(fs) + "KB"
                total_release_space += fs
                total_release_files += 1
                os.remove(fp)

    print("\nTotal delete " + str(total_release_space) + ' KB')
    print("\nTotal delete " + str(total_release_files) + ' files')


delete_trash_files('/Users/chenkan/temp/jl')

@jinkerry
Copy link
Collaborator

如果是小于1024字节的文件,会被忽略的。

@jinkerry
Copy link
Collaborator

如有雷同,纯属巧合

#encoding=utf-8
__author__ = 'jinfeng'

import os


def clean_file(user_dir='.'):
    count = 0
    sum = 0
    for root, dirs, files in os.walk(user_dir):
        for file in files:
            if file.endswith('.trash'):
                full_filename = os.path.join(root, file)
                size = os.path.getsize(full_filename)
                os.remove(full_filename)
                count += 1
                sum += size
            else:
                pass
    print 'count: ', count
    print 'sum: ', sum


if __name__ == "__main__":
    clean_file('e:/clear')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants