{"id":1136472,"date":"2025-01-08T21:36:19","date_gmt":"2025-01-08T13:36:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1136472.html"},"modified":"2025-01-08T21:36:22","modified_gmt":"2025-01-08T13:36:22","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%86%99%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e6%90%9c%e7%b4%a2%e5%99%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1136472.html","title":{"rendered":"\u5982\u4f55\u7528python\u5199\u4e00\u4e2a\u6587\u4ef6\u641c\u7d22\u5668"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100609\/0191e0da-73a3-4b1e-b79b-d45c81a603d5.webp\" alt=\"\u5982\u4f55\u7528python\u5199\u4e00\u4e2a\u6587\u4ef6\u641c\u7d22\u5668\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7f16\u5199\u4e00\u4e2a\u6587\u4ef6\u641c\u7d22\u5668\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528os\u3001fnmatch\u548cargparse\u7b49\u5185\u7f6e\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u901a\u8fc7os\u6a21\u5757\u904d\u5386\u76ee\u5f55\u7ed3\u6784\uff0c\u4f7f\u7528fnmatch\u6a21\u5757\u5339\u914d\u6587\u4ef6\u6a21\u5f0f\uff0c\u5e76\u7528argparse\u6a21\u5757\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/strong>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u6b65\u9aa4\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3\u6587\u4ef6\u641c\u7d22\u5668\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u6587\u4ef6\u641c\u7d22\u5668\u662f\u4e00\u4e2a\u53ef\u4ee5\u5728\u6307\u5b9a\u76ee\u5f55\u53ca\u5176\u5b50\u76ee\u5f55\u4e2d\u67e5\u627e\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u6587\u4ef6\u7684\u5de5\u5177\u3002\u8fd9\u4e9b\u6761\u4ef6\u901a\u5e38\u5305\u62ec\u6587\u4ef6\u540d\u3001\u6587\u4ef6\u7c7b\u578b\u548c\u6587\u4ef6\u5185\u5bb9\u7b49\u3002\u5728Python\u4e2d\uff0c\u5229\u7528\u5185\u7f6e\u6a21\u5757\u5982os\u3001fnmatch\u548cargparse\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6e\u5f00\u53d1\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Python\uff0c\u5e76\u4e14\u73af\u5883\u5df2\u7ecf\u914d\u7f6e\u597d\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5Python\u662f\u5426\u5b89\u88c5\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u904d\u5386\u76ee\u5f55\u7ed3\u6784<\/h3>\n<\/p>\n<p><p><strong>os\u6a21\u5757<\/strong>\u662fPython\u4e2d\u63d0\u4f9b\u7684\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u6a21\u5757\u3002\u901a\u8fc7os.walk()\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u904d\u5386\u4e00\u4e2a\u76ee\u5f55\u53ca\u5176\u6240\u6709\u5b50\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def traverse_directory(directory):<\/p>\n<p>    for dirpath, dirnames, filenames in os.walk(directory):<\/p>\n<p>        print(f&quot;Current directory: {dirpath}&quot;)<\/p>\n<p>        print(f&quot;Subdirectories: {dirnames}&quot;)<\/p>\n<p>        print(f&quot;Files: {filenames}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6587\u4ef6\u6a21\u5f0f\u5339\u914d<\/h3>\n<\/p>\n<p><p><strong>fnmatch\u6a21\u5757<\/strong>\u63d0\u4f9b\u4e86Unix shell\u98ce\u683c\u7684\u6587\u4ef6\u540d\u5339\u914d\u3002\u53ef\u4ee5\u4f7f\u7528fnmatch.fnmatch()\u51fd\u6570\u6765\u5339\u914d\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import fnmatch<\/p>\n<p>def match_pattern(filename, pattern):<\/p>\n<p>    return fnmatch.fnmatch(filename, pattern)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570<\/h3>\n<\/p>\n<p><p><strong>argparse\u6a21\u5757<\/strong>\u7528\u4e8e\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u3002\u53ef\u4ee5\u6307\u5b9a\u641c\u7d22\u76ee\u5f55\u3001\u6587\u4ef6\u6a21\u5f0f\u7b49\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>def parse_arguments():<\/p>\n<p>    parser = argparse.ArgumentParser(description=&quot;File Searcher&quot;)<\/p>\n<p>    parser.add_argument(&quot;directory&quot;, help=&quot;Directory to search&quot;)<\/p>\n<p>    parser.add_argument(&quot;pattern&quot;, help=&quot;File pattern to search for&quot;)<\/p>\n<p>    return parser.parse_args()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6574\u5408\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u529f\u80fd\u6574\u5408\u5230\u4e00\u4e2a\u5b8c\u6574\u7684\u6587\u4ef6\u641c\u7d22\u5668\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import fnmatch<\/p>\n<p>import argparse<\/p>\n<p>def traverse_directory(directory, pattern):<\/p>\n<p>    for dirpath, dirnames, filenames in os.walk(directory):<\/p>\n<p>        for filename in filenames:<\/p>\n<p>            if fnmatch.fnmatch(filename, pattern):<\/p>\n<p>                print(os.path.join(dirpath, filename))<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    args = parse_arguments()<\/p>\n<p>    traverse_directory(args.directory, args.pattern)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f18\u5316\u4e0e\u6269\u5c55<\/h3>\n<\/p>\n<p><h4>1. \u6dfb\u52a0\u66f4\u591a\u641c\u7d22\u6761\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u6269\u5c55\u641c\u7d22\u529f\u80fd\uff0c\u5305\u62ec\u6587\u4ef6\u5927\u5c0f\u3001\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\u7b49\u3002\u4f8b\u5982\uff0c\u901a\u8fc7os.stat()\u51fd\u6570\u83b7\u53d6\u6587\u4ef6\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_file_info(filepath):<\/p>\n<p>    file_info = os.stat(filepath)<\/p>\n<p>    return {<\/p>\n<p>        &quot;size&quot;: file_info.st_size,<\/p>\n<p>        &quot;creation_time&quot;: file_info.st_ctime<\/p>\n<p>    }<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528re\u6a21\u5757\u652f\u6301\u66f4\u590d\u6742\u7684\u5339\u914d\u6761\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def match_regex(filename, regex):<\/p>\n<p>    return re.match(regex, filename)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u591a\u7ebf\u7a0b\u641c\u7d22<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u641c\u7d22\u6548\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u3002Python\u7684threading\u6a21\u5757\u53ef\u4ee5\u5b9e\u73b0\u591a\u7ebf\u7a0b\u641c\u7d22\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def thread_search(directory, pattern):<\/p>\n<p>    threads = []<\/p>\n<p>    for dirpath, dirnames, filenames in os.walk(directory):<\/p>\n<p>        for filename in filenames:<\/p>\n<p>            if fnmatch.fnmatch(filename, pattern):<\/p>\n<p>                thread = threading.Thread(target=print, args=(os.path.join(dirpath, filename),))<\/p>\n<p>                threads.append(thread)<\/p>\n<p>                thread.start()<\/p>\n<p>    for thread in threads:<\/p>\n<p>        thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u7528Python\u6784\u5efa\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6587\u4ef6\u641c\u7d22\u5668\u3002<strong>os\u3001fnmatch\u548cargparse\u6a21\u5757<\/strong>\u662f\u5b9e\u73b0\u6587\u4ef6\u641c\u7d22\u7684\u6838\u5fc3\u5de5\u5177\u3002\u901a\u8fc7\u4e0d\u65ad\u4f18\u5316\u548c\u6269\u5c55\uff0c\u53ef\u4ee5\u6dfb\u52a0\u66f4\u591a\u529f\u80fd\u548c\u63d0\u9ad8\u641c\u7d22\u6548\u7387\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5bf9\u4f60\u7406\u89e3\u548c\u5b9e\u73b0\u6587\u4ef6\u641c\u7d22\u5668\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u4ef6\u641c\u7d22\u5668\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u6587\u4ef6\u641c\u7d22\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684os\u6a21\u5757\u6765\u904d\u5386\u6587\u4ef6\u7cfb\u7edf\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u641c\u7d22\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u6307\u5b9a\u8981\u641c\u7d22\u7684\u76ee\u5f55\u548c\u6587\u4ef6\u540d\u6a21\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528glob\u6a21\u5757\u6765\u5339\u914d\u6587\u4ef6\u540d\uff0c\u6216\u4f7f\u7528fnmatch\u6a21\u5757\u8fdb\u884c\u66f4\u590d\u6742\u7684\u5339\u914d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import os\nimport fnmatch\n\ndef file_search(directory, pattern):\n    matches = []\n    for root, dirnames, filenames in os.walk(directory):\n        for filename in fnmatch.filter(filenames, pattern):\n            matches.append(os.path.join(root, filename))\n    return matches\n\n# \u4f7f\u7528\u793a\u4f8b\nresults = file_search(&#39;\/path\/to\/search&#39;, &#39;*.txt&#39;)\nfor result in results:\n    print(result)\n<\/code><\/pre>\n<p><strong>\u6211\u53ef\u4ee5\u5728\u6587\u4ef6\u641c\u7d22\u5668\u4e2d\u6dfb\u52a0\u54ea\u4e9b\u529f\u80fd\u6765\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\uff1f<\/strong><br \/>\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u529f\u80fd\u6765\u589e\u5f3a\u6587\u4ef6\u641c\u7d22\u5668\u7684\u7528\u6237\u4f53\u9a8c\uff1a  <\/p>\n<ol>\n<li><strong>\u641c\u7d22\u8fc7\u6ee4\u5668<\/strong>\uff1a\u5141\u8bb8\u7528\u6237\u6839\u636e\u6587\u4ef6\u5927\u5c0f\u3001\u4fee\u6539\u65e5\u671f\u7b49\u6761\u4ef6\u8fdb\u884c\u8fc7\u6ee4\u3002  <\/li>\n<li><strong>\u9012\u5f52\u641c\u7d22\u9009\u9879<\/strong>\uff1a\u63d0\u4f9b\u9009\u9879\uff0c\u8ba9\u7528\u6237\u9009\u62e9\u662f\u5426\u5728\u5b50\u76ee\u5f55\u4e2d\u8fdb\u884c\u641c\u7d22\u3002  <\/li>\n<li><strong>\u56fe\u5f62\u7528\u6237\u754c\u9762<\/strong>\uff1a\u4f7f\u7528Tkinter\u6216PyQt\u7b49\u5e93\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684GUI\uff0c\u4f7f\u641c\u7d22\u8fc7\u7a0b\u66f4\u76f4\u89c2\u3002  <\/li>\n<li><strong>\u641c\u7d22\u7ed3\u679c\u5bfc\u51fa<\/strong>\uff1a\u5141\u8bb8\u7528\u6237\u5c06\u641c\u7d22\u7ed3\u679c\u5bfc\u51fa\u4e3a\u6587\u672c\u6587\u4ef6\u6216CSV\u683c\u5f0f\uff0c\u65b9\u4fbf\u540e\u7eed\u67e5\u770b\u3002<\/li>\n<\/ol>\n<p><strong>\u5904\u7406\u5927\u76ee\u5f55\u65f6\uff0c\u5982\u4f55\u4f18\u5316\u6587\u4ef6\u641c\u7d22\u5668\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u4f18\u5316\u6587\u4ef6\u641c\u7d22\u5668\u6027\u80fd\u7684\u51e0\u4e2a\u5efa\u8bae\u5305\u62ec\uff1a  <\/p>\n<ol>\n<li><strong>\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u5904\u7406<\/strong>\uff1a\u4f7f\u7528\u7ebf\u7a0b\u6216\u5f02\u6b65IO\u6765\u52a0\u901f\u641c\u7d22\u8fc7\u7a0b\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u65f6\u3002  <\/li>\n<li><strong>\u7d22\u5f15\u6587\u4ef6\u7cfb\u7edf<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a\u7d22\u5f15\u6570\u636e\u5e93\uff0c\u8bb0\u5f55\u6587\u4ef6\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u4ee5\u4fbf\u5feb\u901f\u68c0\u7d22\u3002  <\/li>\n<li><strong>\u9650\u5236\u641c\u7d22\u6df1\u5ea6<\/strong>\uff1a\u5141\u8bb8\u7528\u6237\u8bbe\u7f6e\u641c\u7d22\u7684\u76ee\u5f55\u6df1\u5ea6\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6df1\u5c42\u904d\u5386\u3002  <\/li>\n<li><strong>\u7f13\u5b58\u7ed3\u679c<\/strong>\uff1a\u5bf9\u6700\u8fd1\u7684\u641c\u7d22\u7ed3\u679c\u8fdb\u884c\u7f13\u5b58\uff0c\u4ee5\u4fbf\u5728\u76f8\u540c\u6761\u4ef6\u4e0b\u518d\u6b21\u641c\u7d22\u65f6\u63d0\u9ad8\u901f\u5ea6\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u6709\u52a9\u4e8e\u63d0\u9ad8\u6587\u4ef6\u641c\u7d22\u5668\u7684\u6548\u7387\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7f16\u5199\u4e00\u4e2a\u6587\u4ef6\u641c\u7d22\u5668\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528os\u3001fnmatch\u548cargparse\u7b49\u5185\u7f6e\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1136476,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136472"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1136472"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136472\/revisions"}],"predecessor-version":[{"id":1136477,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136472\/revisions\/1136477"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1136476"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1136472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1136472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1136472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}