首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

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

2012-03-08 
[请大家提意见]各种意见,甚至代码风格。我写Python不够专业,但是想深造。各位多提意见,不管是风格,还是什么。

[请大家提意见]各种意见,甚至代码风格。
我写Python不够专业,但是想深造。
各位多提意见,不管是风格,还是什么。多谢了。200分酬谢大家。

Python code
import osimport shutildef get_exclude_files(fn):    excluded_files = []    file = open(fn)    excluded_files = file.readlines()    file.close()    return excluded_filesdef 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 countdef 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支持都去掉,能删掉七八成的代码。

热点排行