Created
October 20, 2016 17:26
-
-
Save SkylerHu/588a2cc2ca9c12899645cddf93908137 to your computer and use it in GitHub Desktop.
清除当前/指定目录下所有pyc文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding=utf-8 | |
# Filename: cleanpyc.py | |
# Date:2011-03-26 | |
import os | |
import fnmatch | |
import sys | |
def clearpyc(root, patterns='*',single_level=False, yield_folders=False): | |
""" | |
root: 需要遍历的目录 | |
patterns: 需要查找的文件,以;为分割的字符串 | |
single_level: 是否只遍历单层目录,默认为否 | |
yield_folders: 是否包含目录本身,默认为否 | |
""" | |
patterns = patterns.split(';') | |
for path, subdirs, files in os.walk(root): | |
if yield_folders: | |
files.extend(subdirs) | |
files.sort() | |
for name in files: | |
for pattern in patterns: | |
if fnmatch.fnmatch(name, pattern.strip()): # 去除pattern两端的空格 | |
yield os.path.join(path, name) | |
if single_level: | |
break | |
if __name__ == '__main__': | |
if 2 == len(sys.argv): | |
directory = os.path.join(os.getcwd(), sys.argv[1]) | |
print("参数检查正确") | |
else: | |
directory = os.getcwd() | |
print("取当前目录") | |
print(directory) | |
for path in clearpyc(directory, '*.pyc'): | |
print(path) | |
os.remove(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment