{"id":627,"date":"2014-07-18T15:01:03","date_gmt":"2014-07-18T15:01:03","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=627"},"modified":"2014-07-18T15:01:03","modified_gmt":"2014-07-18T15:01:03","slug":"%e7%94%a8python%e5%bf%ab%e9%80%9f%e6%b8%85%e9%99%a4%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/627.html","title":{"rendered":"\u7528Python\u5feb\u901f\u6e05\u9664\u6587\u4ef6"},"content":{"rendered":"<p>\u56e0\u4e3a\u5728\u7f51\u4e0a\u770b\u5230\u4e00\u7bc7\u6587\u7ae0\uff1a<a href=\"http:\/\/m.blog.csdn.net\/blog\/zf_prm\/29197137\" target=\"_blank\">python\u6e05\u9664pyc\u6587\u4ef6\u7684\u811a\u672c<\/a>\uff0c\u800c\u6211\u4e5f\u6709\u6536\u96c6\u811a\u672c\u7684\u4e60\u60ef\uff08\u5e76\u5728\u6b64\u57fa\u7840\u4e0a\u5bf9\u811a\u672c\u8fdb\u884c\u9002\u5f53\u4fee\u6539\u4ee5\u7b26\u5408\u81ea\u5df1\u7684\u529f\u80fd\u9700\u6c42\uff09\uff0c\u7a0d\u5fae\u4fee\u6539\u4e86\u4e00\u4e0b\u4ee3\u7801\uff0c\u6d4b\u8bd5\u901a\u8fc7\uff0c\u5e76\u4e14\u53ef\u4ee5\u518d\u7a0d\u5fae\u4fee\u6539\u4e00\u4e0b\u4ee3\u7801\u5b9e\u73b0\u5220\u9664\u6307\u5b9a\u540e\u7f00\u7684\u6587\u4ef6\uff1a<\/p>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n# -*- coding: utf-8 -*-\nimport os\nimport fnmatch\nimport sys\n\ndef clearFile(root, patterns='*', single_level=False, yield_folders=False):\n\t\"\"\"\n\troot : \u9700\u8981\u904d\u5386\u7684\u76ee\u5f55\n\tpatterns : \u9700\u8981\u67e5\u627e\u7684\u6587\u4ef6\uff0c\u4ee5';'\u4e3a\u5206\u5272\u7684\u5b57\u7b26\u4e32\n\tsingle_level : \u662f\u5426\u53ea\u904d\u5386\u5355\u5c42\u76ee\u5f55\uff0c\u9ed8\u8ba4\u4e3a\u5426\n\tyield_folders : \u662f\u5426\u5305\u542b\u76ee\u5f55\u672c\u8eab\uff0c\u9ed8\u8ba4\u4e3a\u5426\n\t\"\"\"\n\tpatterns = patterns.split(';')\n\tfor path, subdirs, files in os.walk(root):\n\t\tif yield_folders:\n\t\t\tfiles.extend(subdirs)\n\t\t\tfiles.sort()\n\t\tfor name in files:\n\t\t\tfor pattern in patterns:\n\t\t\t\tif fnmatch.fnmatch(name, pattern.strip()):\t# \u53bb\u9664pattern\u4e24\u7aef\u7684\u7a7a\u683c\n\t\t\t\t\tyield os.path.join(path, name)\n\t\tif single_level:\n\t\t\tbreak\n\nif __name__ == '__main__':\n\tif 2 == len(sys.argv):\n\t\t#print os.path.join(os.getcwd(), sys.argv[1])\n\t\tprint u\"\u53c2\u6570\u68c0\u67e5\u6b63\u786e\"\n\telse:\n\t\tprint \"ntUsage: python \"+sys.argv[0]+\" directory\"\n\t\tsys.exit(0)\n\n\tdirectory = sys.argv[1].strip()\n\tfor path in clearFile(directory, '*.pyc'):\n\t# for path in clearFile(directory, sys.argv[2]):\n\t\tprint path\n\t\tos.remove(path)<\/pre>\n<p>\u4e3b\u8981\u7528\u5230\u7684\u662ffnmatch\u6a21\u5757\u6765\u8fdb\u884c\u7684\u6587\u4ef6\u540d\u7684\u5339\u914d\u3002<\/p>\n<h6>fnmatch\u6a21\u5757\u7684\u4e00\u4e9b\u7b80\u5355\u4ecb\u7ecd\uff1a<\/h6>\n<pre class=\"lang:default decode:true \">In [2]: import fnmatch\n\nIn [3]: fnmatch.\nfnmatch.filter      fnmatch.fnmatch     fnmatch.fnmatchcase fnmatch.re          fnmatch.translate\n\nIn [3]: fnmatch.fnm\nfnmatch.fnmatch     fnmatch.fnmatchcase\n\nIn [3]: fnmatch.fnmatch?\nType:        function\nString form: &lt;function fnmatch at 0x027751B0&gt;\nFile:        c:python27libfnmatch.py\nDefinition:  fnmatch.fnmatch(name, pat)\nDocstring:\nTest whether FILENAME matches PATTERN.\n\nPatterns are Unix shell style:\n\n*       matches everything\n?       matches any single character\n[seq]   matches any character in seq\n[!seq]  matches any char not in seq\n\nAn initial period in FILENAME is not special.\nBoth FILENAME and PATTERN are first case-normalized\nif the operating system requires it.\nIf you don't want this, use fnmatchcase(FILENAME, PATTERN).\n\nIn [4]: fnmatch.re?\nType:        module\nString form: &lt;module 're' from 'C:Python27libre.pyc'&gt;\nFile:        c:python27libre.py\nDocstring:\nSupport for regular expressions (RE).\n\nThis module provides regular expression matching operations similar to\nthose found in Perl.  It supports both 8-bit and Unicode strings; both\nthe pattern and the strings being processed can contain null bytes and\ncharacters outside the US ASCII range.\n\nRegular expressions can contain both special and ordinary characters.\nMost ordinary characters, like \"A\", \"a\", or \"0\", are the simplest\nregular expressions; they simply match themselves.  You can\nconcatenate ordinary characters, so last matches the string 'last'.\n\nThe special characters are:\n    \".\"      Matches any character except a newline.\n    \"^\"      Matches the start of the string.\n    \"$\"      Matches the end of the string or just before the newline at\n             the end of the string.\n    \"*\"      Matches 0 or more (greedy) repetitions of the preceding RE.\n             Greedy means that it will match as many repetitions as possible.\n    \"+\"      Matches 1 or more (greedy) repetitions of the preceding RE.\n    \"?\"      Matches 0 or 1 (greedy) of the preceding RE.\n    *?,+?,?? Non-greedy versions of the previous three special characters.\n    {m,n}    Matches from m to n repetitions of the preceding RE.\n    {m,n}?   Non-greedy version of the above.\n    \"\\\"     Either escapes special characters or signals a special sequence.\n    []       Indicates a set of characters.\n             A \"^\" as the first character indicates a complementing set.\n    \"|\"      A|B, creates an RE that will match either A or B.\n    (...)    Matches the RE inside the parentheses.\n             The contents can be retrieved or matched later in the string.\n    (?iLmsux) Set the I, L, M, S, U, or X flag for the RE (see below).\n    (?:...)  Non-grouping version of regular parentheses.\n    (?P&lt;name&gt;...) The substring matched by the group is accessible by name.\n    (?P=name)     Matches the text matched earlier by the group named name.\n    (?#...)  A comment; ignored.\n    (?=...)  Matches if ... matches next, but doesn't consume the string.\n    (?!...)  Matches if ... doesn't match next.\n    (?&lt;=...) Matches if preceded by ... (must be fixed length).\n    (?&lt;!...) Matches if not preceded by ... (must be fixed length).\n    (?(id\/name)yes|no) Matches yes pattern if the group with id\/name matched,\n                       the (optional) no pattern otherwise.\n\nThe special sequences consist of \"\\\" and a character from the list\nbelow.  If the ordinary character is not on the list, then the\nresulting RE will match the second character.\n    number  Matches the contents of the group of the same number.\n    A       Matches only at the start of the string.\n    Z       Matches only at the end of the string.\n    b       Matches the empty string, but only at the start or end of a word.\n    B       Matches the empty string, but not at the start or end of a word.\n    d       Matches any decimal digit; equivalent to the set [0-9].\n    D       Matches any non-digit character; equivalent to the set [^0-9].\n    s       Matches any whitespace character; equivalent to [ tnrfv].\n    S       Matches any non-whitespace character; equiv. to [^ tnrfv].\n    w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_].\n             With LOCALE, it will match the set [0-9_] plus characters defined\n             as letters for the current locale.\n    W       Matches the complement of w.\n    \\       Matches a literal backslash.\n\nThis module exports the following functions:\n    match    Match a regular expression pattern to the beginning of a string.\n    search   Search a string for the presence of a pattern.\n    sub      Substitute occurrences of a pattern found in a string.\n    subn     Same as sub, but also return the number of substitutions made.\n    split    Split a string by the occurrences of a pattern.\n    findall  Find all occurrences of a pattern in a string.\n    finditer Return an iterator yielding a match object for each match.\n    compile  Compile a pattern into a RegexObject.\n    purge    Clear the regular expression cache.\n    escape   Backslash all non-alphanumerics in a string.\n\nSome of the functions in this module takes flags as optional parameters:\n    I  IGNORECASE  Perform case-insensitive matching.\n    L  LOCALE      Make w, W, b, B, dependent on the current locale.\n    M  MULTILINE   \"^\" matches the beginning of lines (after a newline)\n                   as well as the string.\n                   \"$\" matches the end of lines (before a newline) as well\n                   as the end of the string.\n    S  DOTALL      \".\" matches any character at all, including the newline.\n    X  VERBOSE     Ignore whitespace and comments for nicer looking RE's.\n    U  UNICODE     Make w, W, b, B, dependent on the Unicode locale.\n\nThis module also defines an exception 'error'.\n\nIn [5]:<\/pre>\n<p>&nbsp;<\/p>\n<h6>\u53c2\u8003\u6587\u7ae0\uff1a<\/h6>\n<p><a href=\"http:\/\/m.blog.csdn.net\/blog\/zf_prm\/29197137\" target=\"_blank\">python\u6e05\u9664pyc\u6587\u4ef6\u7684\u811a\u672c<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u56e0\u4e3a\u5728\u7f51\u4e0a\u770b\u5230\u4e00\u7bc7\u6587\u7ae0\uff1apython\u6e05\u9664pyc\u6587\u4ef6\u7684\u811a\u672c\uff0c\u800c\u6211\u4e5f\u6709\u6536\u96c6\u811a\u672c\u7684\u4e60\u60ef\uff08\u5e76\u5728\u6b64\u57fa\u7840\u4e0a\u5bf9\u811a\u672c\u8fdb\u884c\u9002\u5f53\u4fee [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,12],"tags":[243,8],"class_list":["post-627","post","type-post","status-publish","format-standard","hentry","category-programing","category-tools","tag-fnmatch","tag-python"],"views":2017,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=627"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/627\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}