{"id":1066866,"date":"2024-12-31T16:30:07","date_gmt":"2024-12-31T08:30:07","guid":{"rendered":""},"modified":"2024-12-31T16:30:10","modified_gmt":"2024-12-31T08:30:10","slug":"python%e4%b8%ados%e5%ba%93%e5%a6%82%e4%bd%95%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1066866.html","title":{"rendered":"python\u4e2dos\u5e93\u5982\u4f55\u4e0b\u8f7d\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/b3fbb5d9-12b3-4d59-9351-df79adad2ba7.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2dos\u5e93\u5982\u4f55\u4e0b\u8f7d\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528os\u5e93\u5e76\u4e0d\u662f\u4e0b\u8f7d\u6587\u4ef6\u7684\u6700\u4f73\u9009\u62e9\u3002\u66f4\u63a8\u8350\u4f7f\u7528\u7684\u5e93\u662frequests\u3001urllib\u6216wget\uff0c\u4f46\u53ef\u4ee5\u7ed3\u5408os\u5e93\u6765\u5904\u7406\u6587\u4ef6\u7684\u5b58\u653e\u8def\u5f84\u3001\u521b\u5efa\u76ee\u5f55\u7b49\u64cd\u4f5c\u3002<\/strong>\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u4e0b\u8f7d\u6587\u4ef6\uff0c\u5e76\u7ed3\u5408os\u5e93\u8fdb\u884c\u6587\u4ef6\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528requests\u5e93\u4e0b\u8f7d\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>requests\u5e93\u662fPython\u4e2d\u6700\u5e38\u7528\u7684HTTP\u5e93\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u4e0b\u8f7d\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u5b89\u88c5requests\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5requests\u5e93\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5\u5b83\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u4e0b\u8f7d\u6587\u4ef6\u5e76\u4fdd\u5b58<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import requests<\/p>\n<p>def download_file(url, save_path):<\/p>\n<p>    response = requests.get(url, stream=True)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        with open(save_path, &#39;wb&#39;) as file:<\/p>\n<p>            for chunk in response.iter_content(chunk_size=1024):<\/p>\n<p>                file.write(chunk)<\/p>\n<p>        print(f&quot;File downloaded successfully and saved to {save_path}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to download file. Status code: {response.status_code}&quot;)<\/p>\n<h2><strong>Example usage<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com\/file.zip&#39;<\/p>\n<p>save_path = os.path.join(&#39;downloads&#39;, &#39;file.zip&#39;)<\/p>\n<p>os.makedirs(os.path.dirname(save_path), exist_ok=True)<\/p>\n<p>download_file(url, save_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>requests.get<\/strong>\uff1a\u7528\u4e8e\u53d1\u9001HTTP GET\u8bf7\u6c42\u3002<\/li>\n<li><strong>response.iter_content<\/strong>\uff1a\u4ee5\u6d41\u7684\u65b9\u5f0f\u4e0b\u8f7d\u6587\u4ef6\uff0c\u907f\u514d\u5360\u7528\u8fc7\u591a\u5185\u5b58\u3002<\/li>\n<li><strong>os.path.join<\/strong> \u548c <strong>os.makedirs<\/strong>\uff1a\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u8def\u5f84\u548c\u521b\u5efa\u76ee\u5f55\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528urllib\u5e93\u4e0b\u8f7d\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>urllib\u5e93\u662fPython\u81ea\u5e26\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4e0b\u8f7d\u6587\u4ef6\u5e76\u4fdd\u5b58<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import urllib.request<\/p>\n<p>def download_file(url, save_path):<\/p>\n<p>    try:<\/p>\n<p>        urllib.request.urlretrieve(url, save_path)<\/p>\n<p>        print(f&quot;File downloaded successfully and saved to {save_path}&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Failed to download file. Error: {e}&quot;)<\/p>\n<h2><strong>Example usage<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com\/file.zip&#39;<\/p>\n<p>save_path = os.path.join(&#39;downloads&#39;, &#39;file.zip&#39;)<\/p>\n<p>os.makedirs(os.path.dirname(save_path), exist_ok=True)<\/p>\n<p>download_file(url, save_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>urllib.request.urlretrieve<\/strong>\uff1a\u76f4\u63a5\u4e0b\u8f7d\u6587\u4ef6\u5e76\u4fdd\u5b58\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528wget\u5e93\u4e0b\u8f7d\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>wget\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u4e0b\u8f7d\u6587\u4ef6\u7684\u5c0f\u5de5\u5177\uff0c\u975e\u5e38\u6613\u7528\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5wget\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5wget\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install wget<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u4e0b\u8f7d\u6587\u4ef6\u5e76\u4fdd\u5b58<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import wget<\/p>\n<p>def download_file(url, save_path):<\/p>\n<p>    try:<\/p>\n<p>        wget.download(url, save_path)<\/p>\n<p>        print(f&quot;\\nFile downloaded successfully and saved to {save_path}&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Failed to download file. Error: {e}&quot;)<\/p>\n<h2><strong>Example usage<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com\/file.zip&#39;<\/p>\n<p>save_path = os.path.join(&#39;downloads&#39;, &#39;file.zip&#39;)<\/p>\n<p>os.makedirs(os.path.dirname(save_path), exist_ok=True)<\/p>\n<p>download_file(url, save_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>wget.download<\/strong>\uff1a\u7528\u4e8e\u4e0b\u8f7d\u6587\u4ef6\u5e76\u4fdd\u5b58\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u7ed3\u5408os\u5e93\u8fdb\u884c\u6587\u4ef6\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0b\u8f7d\u6587\u4ef6\u4e4b\u5916\uff0cos\u5e93\u5728\u5904\u7406\u6587\u4ef6\u8def\u5f84\u548c\u76ee\u5f55\u64cd\u4f5c\u65b9\u9762\u4e5f\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684os\u5e93\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><h4>4.1 \u521b\u5efa\u76ee\u5f55<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>directory = &#39;downloads&#39;<\/p>\n<p>if not os.path.exists(directory):<\/p>\n<p>    os.makedirs(directory)<\/p>\n<p>    print(f&quot;Directory {directory} created&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;Directory {directory} already exists&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;downloads\/file.zip&#39;<\/p>\n<p>if os.path.exists(file_path):<\/p>\n<p>    print(f&quot;File {file_path} exists&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;File {file_path} does not exist&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.3 \u5220\u9664\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;downloads\/file.zip&#39;<\/p>\n<p>if os.path.exists(file_path):<\/p>\n<p>    os.remove(file_path)<\/p>\n<p>    print(f&quot;File {file_path} deleted&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;File {file_path} does not exist&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u7ed3\u5408\u4ee5\u4e0a\u5185\u5bb9\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528requests\u5e93\u4e0b\u8f7d\u6587\u4ef6\uff0c\u5e76\u7ed3\u5408os\u5e93\u8fdb\u884c\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import requests<\/p>\n<p>def download_file(url, save_path):<\/p>\n<p>    response = requests.get(url, stream=True)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        with open(save_path, &#39;wb&#39;) as file:<\/p>\n<p>            for chunk in response.iter_content(chunk_size=1024):<\/p>\n<p>                file.write(chunk)<\/p>\n<p>        print(f&quot;File downloaded successfully and saved to {save_path}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Failed to download file. Status code: {response.status_code}&quot;)<\/p>\n<p>def main():<\/p>\n<p>    url = &#39;https:\/\/example.com\/file.zip&#39;<\/p>\n<p>    save_path = os.path.join(&#39;downloads&#39;, &#39;file.zip&#39;)<\/p>\n<p>    # Create directory if it doesn&#39;t exist<\/p>\n<p>    if not os.path.exists(os.path.dirname(save_path)):<\/p>\n<p>        os.makedirs(os.path.dirname(save_path))<\/p>\n<p>        print(f&quot;Directory {os.path.dirname(save_path)} created&quot;)<\/p>\n<p>    # Check if file already exists<\/p>\n<p>    if os.path.exists(save_path):<\/p>\n<p>        print(f&quot;File {save_path} already exists&quot;)<\/p>\n<p>    else:<\/p>\n<p>        download_file(url, save_path)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>os.path.exists<\/strong>\uff1a\u68c0\u67e5\u76ee\u5f55\u548c\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/li>\n<li><strong>os.makedirs<\/strong>\uff1a\u521b\u5efa\u76ee\u5f55\u3002<\/li>\n<li><strong>requests.get<\/strong>\uff1a\u4e0b\u8f7d\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u5e0c\u671b\u4f60\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5404\u79cd\u5e93\u4e0b\u8f7d\u6587\u4ef6\uff0c\u5e76\u7ed3\u5408os\u5e93\u8fdb\u884c\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7684os\u5e93\u4e0b\u8f7d\u6587\u4ef6\uff1f<\/strong><br \/>os\u5e93\u672c\u8eab\u5e76\u4e0d\u63d0\u4f9b\u76f4\u63a5\u4e0b\u8f7d\u6587\u4ef6\u7684\u529f\u80fd\uff0c\u5b83\u4e3b\u8981\u7528\u4e8e\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u3002\u5982\u679c\u60a8\u60f3\u4e0b\u8f7d\u6587\u4ef6\uff0c\u901a\u5e38\u9700\u8981\u7ed3\u5408\u5176\u4ed6\u5e93\uff0c\u4f8b\u5982requests\u5e93\u6765\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\u3002\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u4e0b\u8f7d\u6587\u4ef6\uff0c\u7136\u540e\u5229\u7528os\u5e93\u4fdd\u5b58\u5230\u672c\u5730\u6587\u4ef6\u7cfb\u7edf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\nimport os\n\nurl = &#39;http:\/\/example.com\/file.zip&#39;\nresponse = requests.get(url)\n\n# \u786e\u4fdd\u6587\u4ef6\u5939\u5b58\u5728\nos.makedirs(&#39;downloads&#39;, exist_ok=True)\n\nwith open(os.path.join(&#39;downloads&#39;, &#39;file.zip&#39;), &#39;wb&#39;) as f:\n    f.write(response.content)\n<\/code><\/pre>\n<p><strong>os\u5e93\u5728\u6587\u4ef6\u7ba1\u7406\u4e2d\u6709\u54ea\u4e9b\u91cd\u8981\u7684\u529f\u80fd\uff1f<\/strong><br \/>os\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u529f\u80fd\uff0c\u6bd4\u5982\u521b\u5efa\u3001\u5220\u9664\u3001\u91cd\u547d\u540d\u6587\u4ef6\u6216\u6587\u4ef6\u5939\uff0c\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u7b49\u3002\u901a\u8fc7os\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528os.listdir()\u5217\u51fa\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\uff0c\u4f7f\u7528os.remove()\u5220\u9664\u6587\u4ef6\uff0c\u4ee5\u53ca\u4f7f\u7528os.rename()\u91cd\u547d\u540d\u6587\u4ef6\u3002\u8fd9\u4e9b\u529f\u80fd\u4f7f\u5f97\u6587\u4ef6\u7ba1\u7406\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u4e0b\u8f7d\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u4e0b\u8f7d\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528os\u5e93\u4e2d\u7684\u529f\u80fd\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4f7f\u7528os.path\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u4f7f\u7528os.remove()\u6765\u5220\u9664\u4e0d\u9700\u8981\u7684\u6587\u4ef6\uff0c\u6216\u4f7f\u7528os.rename()\u6765\u91cd\u547d\u540d\u6587\u4ef6\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528os.chdir()\u66f4\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u4ee5\u4fbf\u5728\u4e0d\u540c\u7684\u76ee\u5f55\u4e2d\u64cd\u4f5c\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528os\u5e93\u5e76\u4e0d\u662f\u4e0b\u8f7d\u6587\u4ef6\u7684\u6700\u4f73\u9009\u62e9\u3002\u66f4\u63a8\u8350\u4f7f\u7528\u7684\u5e93\u662frequests\u3001urllib\u6216wget [&hellip;]","protected":false},"author":3,"featured_media":1066877,"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\/1066866"}],"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=1066866"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1066866\/revisions"}],"predecessor-version":[{"id":1066879,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1066866\/revisions\/1066879"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1066877"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1066866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1066866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1066866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}