{"id":1794,"date":"2020-07-01T05:21:22","date_gmt":"2020-06-30T21:21:22","guid":{"rendered":"http:\/\/39.97.231.10\/?p=1794"},"modified":"2020-07-01T05:21:22","modified_gmt":"2020-06-30T21:21:22","slug":"%e5%9c%a8-python-%e4%b8%ad%e5%a6%82%e4%bd%95%e5%a4%8d%e5%88%b6%e3%80%81%e7%a7%bb%e5%8a%a8%e3%80%81%e6%94%b9%e5%90%8d%e4%bb%a5%e5%8f%8a%e5%88%a0%e9%99%a4%e6%96%87%e4%bb%b6%e6%88%96%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=1794","title":{"rendered":"\u5728 Python \u4e2d\u5982\u4f55\u590d\u5236\u3001\u79fb\u52a8\u3001\u6539\u540d\u4ee5\u53ca\u5220\u9664\u6587\u4ef6\u6216\u6587\u4ef6\u5939"},"content":{"rendered":"<div>\n<div>\n<div>\n<div>\u8981\u5b9e\u73b0\u590d\u5236\u3001\u79fb\u52a8\u3001\u6539\u540d\u4ee5\u53ca\u5220\u9664\u6587\u4ef6\u6216\u6587\u4ef6\u5939\uff0c\u9700\u8981\u7528\u5230 shutil \u6a21\u5757\uff0cshutil \u662f shell util \u7684\u7b80\u5199\u5f62\u5f0f\uff0c\u8868\u793a shell \u5de5\u5177\u3002<\/div>\n<\/div>\n<\/div>\n<\/div>\n<article>\n<h2>1 \u590d\u5236\u6587\u4ef6\u6216\u6587\u4ef6\u5939<\/h2>\n<p>\u8c03\u7528 shutil.copy(source, destination) \u6765\u5b9e\u73b0\u590d\u5236\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u529f\u80fd\uff0c\u4f9d\u636e destination \u8fdb\u884c\u533a\u5206\uff1a<\/p>\n<ol>\n<li>\u5982\u679c destination \u662f\u6587\u4ef6\u5939\uff0c\u90a3\u4e48\u4f1a\u5c06\u8def\u5f84 source \u5904\u7684\u6587\u4ef6\u590d\u5236\u5230\u8def\u5f84 destination \u5904\u7684\u6587\u4ef6\u5939\u3002<\/li>\n<li>\u5982\u679c destination \u662f\u4e00\u4e2a\u6587\u4ef6\u540d\uff0c\u90a3\u4e48\u4f1a\u5c06\u8def\u5f84 source \u5904\u7684\u6587\u4ef6\u6539\u4e3a\u65b0\u7684\u540d\u5b57\u3002<\/li>\n<\/ol>\n<div>\n<pre>import shutil, os\n\\'\\'\\'\n\u590d\u5236\u6587\u4ef6\u5939\u548c\u6587\u4ef6\n\\'\\'\\'\nos.chdir(\\'F:\\\\\\\\temp\\')\n# \u628a\u6587\u4ef6\u590d\u5236\u5230\u6587\u4ef6\u5939\npath = shutil.copy(\\'F:\\\\\\\\temp\\\\\\\\config.xml\\', \\'F:\\\\\\\\temp\\\\\\\\1\\')\nprint(\\'path=\\', path)\n# \u628a\u6587\u4ef6\u590d\u5236\u5230\u6587\u4ef6\u5939\uff0c\u5e76\u6539\u540d\npath = shutil.copy(\\'F:\\\\\\\\temp\\\\\\\\config.xml\\', \\'F:\\\\\\\\temp\\\\\\\\1\\\\\\\\conf.xml\\')\nprint(\\'path=\\', path)\n<\/pre>\n<\/div>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<blockquote><p>path= F:\\\\temp\\\\1\\\\config.xml<br \/>\npath= F:\\\\temp\\\\1\\\\conf.xml<\/p><\/blockquote>\n<div>\n<div>\n<div><\/div>\n<div data-width=\\\"217\\\" data-height=\\\"102\\\"><img src=\\\"https:\/\/upload-images.jianshu.io\/upload_images\/3386108-95e7b79a2c9c1b2a.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/217\/format\/webp\\\" data-original-src=\\\"\/\/upload-images.jianshu.io\/upload_images\/3386108-95e7b79a2c9c1b2a.png\\\" data-original-width=\\\"217\\\" data-original-height=\\\"102\\\" data-original-format=\\\"image\/png\\\" data-original-filesize=\\\"2304\\\" data-image-index=\\\"0\\\" \/><\/div>\n<\/div>\n<\/div>\n<p>\u6ce8\u610f\uff1a\u00a0\u6307\u5b9a\u590d\u5236\u7684\u6587\u4ef6\u5939\u5fc5\u987b\u5b58\u5728\uff0c\u5426\u5219\u4f1a\u629b\u51fa FileNotFoundError\u3002<br \/>\nshutil \u7684 copytree(source, destination) \u65b9\u6cd5\u4f1a\u590d\u5236\u6574\u4e2a\u6587\u4ef6\u5939\uff0c\u5305\u62ec\u5b83\u6240\u5305\u542b\u7684\u6240\u6709\u6587\u4ef6\u5939\u548c\u6587\u4ef6\u3002source<br \/>\n\u6307\u5b9a\u6e90\u6587\u4ef6\u5939\uff0cdestination \u6307\u5b9a\u65b0\u7684\u6587\u4ef6\u5939\u3002source \u548c destination \u5165\u53c2\u90fd\u662f\u5b57\u7b26\u4e32\u3002\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de\u65b0\u6587\u4ef6\u5939\u7684\u8def\u5f84\u3002destination \u5982\u679c\u4e0d\u5b58\u5728\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\u3002\u8bf7\u770b\u4e0b\u4f8b\uff1a<\/p>\n<div>\n<pre># \u590d\u5236\u6574\u4e2a\u6587\u4ef6\u5939\npath = shutil.copytree(\\'F:\\\\\\\\temp\\\\\\\\1\\', \\'F:\\\\\\\\temp\\\\\\\\1_backup\\')\nprint(\\'path=\\', path)\n<\/pre>\n<\/div>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<blockquote><p>path= F:\\\\temp\\\\1_backup<\/p><\/blockquote>\n<h2>2 \u79fb\u52a8\u6587\u4ef6\u6216\u6587\u4ef6\u5939<\/h2>\n<p>shutil.move(source, destination) \u65b9\u6cd5\u4f1a\u5c06\u8def\u5f84 source \u5904\u7684\u6587\u4ef6\u79fb\u52a8\u5230\u8def\u5f84 destination\uff0c\u5e76\u8fd4\u56de\u65b0\u4f4d\u7f6e\u7684\u7edd\u5bf9\u8def\u5f84\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n\u5982\u679c destination \u6307\u5411\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c source \u5904\u7684\u6587\u4ef6\u5c06\u79fb\u52a8\u5230 destination \u4e2d\uff0c \u5e76\u4fdd\u6301\u539f\u6765\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<div>\n<pre>import shutil\n\\'\\'\\'\n\u79fb\u52a8\u6587\u4ef6\u6216\u6587\u4ef6\u5939\n\\'\\'\\'\n# \u79fb\u52a8\u6587\u4ef6\npath = shutil.move(\\'F:\\\\\\\\temp\\\\\\\\bird2.log\\',\\'F:\\\\\\\\temp\\\\\\\\conf\\')\nprint(\\'path=\\', path)\n# \u79fb\u52a8\u6587\u4ef6\u5939\npath = shutil.move(\\'F:\\\\\\\\temp\\\\\\\\python\\', \\'F:\\\\\\\\temp\\\\\\\\conf\\')\nprint(\\'path=\\', path)\n<\/pre>\n<\/div>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<blockquote><p>path= F:\\\\temp\\\\conf\\\\bird2.log<br \/>\npath= F:\\\\temp\\\\conf\\\\python<\/p><\/blockquote>\n<p>\u6ce8\u610f\uff1a<\/p>\n<ol>\n<li>\u5982\u679c\u5728 destination \u4e2d\u539f\u6765\u5df2\u7ecf\u5b58\u5728\u4e86\u4e00\u4e2a\u540c\u540d\u6587\u4ef6\uff08\u4e0e source \u76f8\u540c\uff09\uff0c\u90a3\u4e48\u8fd9\u4e2a\u6587\u4ef6\u5c31\u4f1a\u88ab\u8986\u5199\u3002<\/li>\n<li>\u5982\u679c destination \u6587\u4ef6\u5939\u4e0d\u5b58\u5728\uff0c\u90a3\u4e48 shutil \u5c31\u4f1a\u65b0\u5efa\u4e00\u4e2a destination \u6587\u4ef6\uff0c\u8fd9\u4e00\u70b9\u5bb9\u6613\u9020\u6210\u56f0\u6270\u3002<\/li>\n<\/ol>\n<h2>3 \u6c38\u4e45\u5220\u9664\u6587\u4ef6\u6216\u6587\u4ef6\u5939<\/h2>\n<p>os \u6a21\u5757\u4e2d\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5220\u9664\u4e00\u4e2a\u6587\u4ef6\u6216\u4e00\u4e2a\u7a7a\u6587\u4ef6\u5939\u3002\u800c shutil \u66f4\u5f3a\u5927\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5220\u9664\u4e00\u4e2a\u975e\u7a7a\u6587\u4ef6\u5939\uff01<\/p>\n<table>\n<thead>\n<tr>\n<th>\u65b9\u6cd5<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>os.unlink(path)<\/td>\n<td>\u5220\u9664 path \u5904\u7684\u6587\u4ef6\u3002<\/td>\n<\/tr>\n<tr>\n<td>os.rmdir(path)<\/td>\n<td>\u5220\u9664 path \u5904\u7684\u6587\u4ef6\u5939\uff0c\u8fd9\u4e2a\u6587\u4ef6\u5939\u5fc5\u987b\u662f\u7a7a\u7684\u6587\u4ef6\u5939\u3002<\/td>\n<\/tr>\n<tr>\n<td>shutil.rmtree(path)<\/td>\n<td>\u5220\u9664 path \u5904\u7684\u6587\u4ef6\u5939\uff0c\u8fd9\u4e2a\u6587\u4ef6\u5939\u5185\u6240\u5305\u542b\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u90fd\u4f1a\u88ab\u5220\u9664\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6ce8\u610f\uff1a\u00a0\u56e0\u4e3a\u662f\u6c38\u4e45\u5220\u9664\uff0c\u6240\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u4e00\u5b9a\u8981\u5c0f\u5fc3\uff01\u5efa\u8bae\u8c03\u8bd5\u7a0b\u5e8f\u65f6\uff0c \u5148\u6ce8\u91ca\u6389\u8fd9\u4e9b\u5220\u9664\u65b9\u6cd5\uff0c<br \/>\n\u7136\u540e\u52a0\u4e0a print()\uff0c \u628a\u8981\u88ab\u5220\u9664\u7684\u6587\u4ef6\u6253\u5370\u51fa\u6765\uff0c\u786e\u8ba4\u540e\uff0c\u518d\u6267\u884c\u3002<\/p>\n<div>\n<pre>import os\n\\'\\'\\'\n\u6c38\u4e45\u5220\u9664\u6587\u4ef6\u548c\u6587\u4ef6\u5939\n\\'\\'\\'\nos.chdir(\\'F:\\\\\\\\temp\\\\\\\\conf\\\\\\\\\\')\nfor filename in os.listdir():\n    if filename.endswith(\\'.log\\'):\n        print(filename)\n        #os.unlink(filename)\n<\/pre>\n<\/div>\n<p>\u6253\u5370\u51fa\u6765\u7684\u6587\u4ef6\u5217\u8868\u786e\u8ba4\u65e0\u8bef\u540e\uff0c\u518d\u6267\u884c os.unlink(filename) \u6267\u884c\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<h2>4 \u5b89\u5168\u5220\u9664\u6587\u4ef6\u6216\u6587\u4ef6\u5939<\/h2>\n<p>send2trash \u6a21\u5757\u4f1a\u5c06\u6587\u4ef6\u5939\u6216\u6587\u4ef6\u53d1\u9001\u5230\u8ba1\u7b97\u673a\u7684\u56de\u6536\u7ad9\u3002\u9996\u5148\uff0c\u5b89\u88c5\u5b83\uff1a<\/p>\n<div>\n<pre>pip install send2trash\n<\/pre>\n<\/div>\n<p>\u5b89\u88c5\u6210\u529f\u540e\uff0c\u8c03\u7528 send2trash.send2trash \u65b9\u6cd5\uff0c\u5c31\u53ef\u4ee5\u628a\u6587\u4ef6\u5939\u6216\u6587\u4ef6\u53d1\u9001\u5230\u8ba1\u7b97\u673a\u7684\u56de\u6536\u7ad9\u3002\u8bf7\u770b\u4e0b\u4f8b\uff1a<\/p>\n<div>\n<pre>import os\nimport send2trash\n\\'\\'\\'\n\u5c06\u6587\u4ef6\u5939\u6216\u6587\u4ef6\u53d1\u9001\u5230\u8ba1\u7b97\u673a\u7684\u56de\u6536\u7ad9\n\\'\\'\\'\nos.chdir(\\'F:\\\\\\\\temp\\\\\\\\conf\\\\\\\\\\')\nsend2trash.send2trash(\\'python\\')\n<\/pre>\n<\/div>\n<div>\n<div>\n<div><\/div>\n<div data-width=\\\"538\\\" data-height=\\\"154\\\"><img src=\\\"https:\/\/upload-images.jianshu.io\/upload_images\/3386108-4f446fa04a2cad24.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/538\/format\/webp\\\" data-original-src=\\\"\/\/upload-images.jianshu.io\/upload_images\/3386108-4f446fa04a2cad24.png\\\" data-original-width=\\\"538\\\" data-original-height=\\\"154\\\" data-original-format=\\\"image\/png\\\" data-original-filesize=\\\"32207\\\" data-image-index=\\\"1\\\" \/><\/div>\n<\/div>\n<\/div>\n<p>\u5efa\u8bae\u4f7f\u7528 send2trash.send2trash() \u51fd\u6570\u6765\u5220\u9664\u6587\u4ef6\u6216\u6587\u4ef6\u5939\uff0c\u56e0\u4e3a\u4ee5\u540e\u8fd8\u53ef\u4ee5\u4ece\u56de\u6536\u7ad9\u8fd8\u539f\u3002\u4f46\u8fd9\u6837\u505a\uff0c\u4e0d<br \/>\n\u4f1a\u91ca\u653e\u78c1\u76d8\u7a7a\u95f4\u3002\u5982\u679c\u6211\u4eec\u8fd8\u662f\u5e0c\u671b\u7a0b\u5e8f\u91ca\u653e\u78c1\u76d8\u7a7a\u95f4\uff0c \u5c31\u8981\u7528 os \u548c shutil \u6765\u5220\u9664\u6587\u4ef6\u548c<br \/>\n\u6587\u4ef6\u5939\uff08\u8bb0\u5f97\u4f7f\u7528\u4e4b\u524d\u63d0\u51fa\u7684 print \u6280\u5de7\uff09\u3002\u8fd8\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\uff0c send2trash() \u51fd\u6570\u53ea\u80fd\u5c06\u6587\u4ef6\u9001\u5230\u56de\u6536\u7ad9\uff0c \u4f46\u4e0d\u80fd\u4ece\u56de\u6536\u7ad9\u4e2d\u6062\u590d\u6587\u4ef6\u3002<br \/>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u5b9e\u73b0\u590d\u5236\u3001\u79fb\u52a8\u3001\u6539\u540d\u4ee5\u53ca\u5220\u9664\u6587\u4ef6\u6216\u6587\u4ef6\u5939\uff0c\u9700\u8981\u7528\u5230 shutil \u6a21\u5757\uff0cshutil \u662f shell uti &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=1794\"> <span class=\"screen-reader-text\">\u5728 Python \u4e2d\u5982\u4f55\u590d\u5236\u3001\u79fb\u52a8\u3001\u6539\u540d\u4ee5\u53ca\u5220\u9664\u6587\u4ef6\u6216\u6587\u4ef6\u5939<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[106,126],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"\u8981\u5b9e\u73b0\u590d\u5236\u3001\u79fb\u52a8\u3001\u6539\u540d\u4ee5\u53ca\u5220\u9664\u6587\u4ef6\u6216\u6587\u4ef6\u5939\uff0c\u9700\u8981\u7528\u5230 shutil \u6a21\u5757\uff0cshutil \u662f shell uti&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1794"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1794"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1794\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1794"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}