专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Perl/Python

[请大家提意见]各种意见,甚至代码风格。解决方案

发布时间:2011-06-29 20:09:19 文章来源:www.iduyao.cn 采编人员:星星草
[请大家提意见]各种意见,甚至代码风格。
我写Python不够专业,但是想深造。
各位多提意见,不管是风格,还是什么。多谢了。200分酬谢大家。

Python code


import os
import shutil

def get_exclude_files(fn):
    excluded_files = []
    file = open(fn)
    excluded_files = file.readlines()
    file.close()
    return excluded_files

def remove_unused_file(file):
    for ext in ['.ncb', '.exe', '.ilk', '.pdb', '.obj', '.idb', '.user', '.aps', '.dep', '.pch', '.res', '.suo']:
        if (file.endswith(ext)):
            os.remove(file)




def remove_dir_files(cur):
    count = 0
    files = os.listdir(cur)
    excluded = os.path.join(cur, "exclude.files")
    excluded_files = []
    if (os.path.exists(excluded)):
        excluded_files = get_exclude_files(excluded)
    for fn in files:
        if (fn in excluded_files):
            continue
        file = os.path.join(cur, fn)
        print(file)
        if os.path.isfile(file):
            remove_unused_file(file)
        else:
            paths = os.listdir( cur )
            count += remove_dir_files(file)
    return count


def main():
    count = remove_dir_files(os.getcwd())

    print(count)

if __name__ == '__main__':
    main()




------解决方案--------------------
写得挺清爽啊
代码格式上:

------解决方案--------------------
1. bug:
readlines()返回的行带有'\n',所以下面一句中的判断总是不会为真:
Python code
        if (fn in excluded_files):
            continue

------解决方案--------------------
就是你想忽略某个文件,还要在这个目录下另外加一个exclude.files文件,比较麻烦啊。如果真的有意义的话,不妨用类似.gitignore那样的方式。但我看你删的都是编译的临时文件,貌似没什么ignore的必要。这样的话反倒不如把你代码里exclude.files支持都去掉,能删掉七八成的代码。
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: