{"id":2692,"date":"2024-09-30T20:17:40","date_gmt":"2024-09-30T11:17:40","guid":{"rendered":"https:\/\/secondlife.lol\/?p=2692"},"modified":"2024-09-29T20:54:19","modified_gmt":"2024-09-29T11:54:19","slug":"python-directory-exploration-os-module","status":"publish","type":"post","link":"https:\/\/secondlife.lol\/zh\/python-directory-exploration-os-module\/","title":{"rendered":"\u5904\u7406 Python \u76ee\u5f55\u7ed3\u6784\uff1a\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757\u5bfc\u822a\u6587\u4ef6\u548c\u6587\u4ef6\u5939"},"content":{"rendered":"<p class=\"wp-block-paragraph\">\u8bb8\u591a\u521d\u5b66\u8005\u5728\u521a\u5f00\u59cb\u5b66\u4e60 Python \u65f6\u90fd\u4f1a\u611f\u5230\u8ff7\u832b\uff0c\u4e0d\u77e5\u9053\u5982\u4f55\u6d4f\u89c8\u76ee\u5f55\u7ed3\u6784\u548c\u5904\u7406\u6587\u4ef6\u3002\u7136\u800c\uff0c\u4e86\u89e3 Python \u76ee\u5f55\u7ed3\u6784\u5e76\u4f7f\u7528 <a href=\"https:\/\/docs.python.org\/ko\/3\/library\/os.html\" data-type=\"link\" data-id=\"https:\/\/docs.python.org\/ko\/3\/library\/os.html\" target=\"_blank\" rel=\"noopener\">\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757<\/a>\u8ba9\u6d4f\u89c8\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u53d8\u5f97\u8f7b\u800c\u6613\u4e3e\u3002\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u5411\u4f60\u5c55\u793a\u5982\u4f55\u4f7f\u7528<strong> \u5982\u4f55\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757\u6d4f\u89c8\u6587\u4ef6\u548c\u76ee\u5f55<\/strong>\u8ba9\u4f60\u4e5f\u80fd\u9ad8\u6548\u5904\u7406\u6587\u4ef6\uff0c\u8f7b\u677e\u7ba1\u7406\u590d\u6742\u7684\u76ee\u5f55\u7ed3\u6784\u3002<\/p>\n\n\n<style>.kb-image2692_90f2a2-8a .kb-image-has-overlay:after{opacity:0.3;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;}.kb-image2692_90f2a2-8a img.kb-img, .kb-image2692_90f2a2-8a .kb-img img{border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}<\/style>\n<div class=\"wp-block-kadence-image kb-image2692_90f2a2-8a\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"600\" height=\"300\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-6-1-600x300.jpg\" alt=\"python \ub514\ub809\ud1a0\ub9ac \ud3ec\uc2a4\ud2b8 \uadf8\ub9bc\" class=\"kb-img wp-image-2693\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-6-1-600x300.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-6-1-300x150.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-6-1-768x384.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-6-1.jpg 1056w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure><\/div>\n\n\n<style>.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{padding-top:var(--global-kb-spacing-sm, 1.5rem);padding-right:var(--global-kb-spacing-sm, 1.5rem);padding-bottom:var(--global-kb-spacing-sm, 1.5rem);padding-left:var(--global-kb-spacing-sm, 1.5rem);border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title-wrap{padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title-wrap{color:var(--global-palette2, #2B6CB0);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{color:var(--global-palette2, #2B6CB0);font-size:28px;font-weight:regular;font-style:normal;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap .kb-table-of-content-list{color:var(--global-palette1, #3182CE);line-height:2em;font-weight:regular;font-style:normal;margin-top:var(--global-kb-spacing-sm, 1.5rem);margin-right:0px;margin-bottom:0px;margin-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap .kb-table-of-content-list .kb-table-of-contents__entry:hover{color:var(--global-palette6, #718096);}@media all and (max-width: 1024px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);}}@media all and (max-width: 1024px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{font-size:28px;}}@media all and (max-width: 767px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{font-size:28px;}}<\/style>\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757\u8fdb\u884c\u76ee\u5f55\u5bfc\u822a\u7684\u57fa\u7840\u77e5\u8bc6<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u5728 Python \u4e2d\u5904\u7406\u76ee\u5f55\u7ed3\u6784\uff0c\u4f7f\u7528 <strong>\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757<\/strong>os \u6a21\u5757\u662f Python \u4e2d\u7684\u4e00\u4e2a\u6807\u51c6\u5e93\uff0c\u53ef\u8ba9\u60a8\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\uff0c\u5904\u7406\u4e0e\u6587\u4ef6\u548c\u76ee\u5f55\u76f8\u5173\u7684\u4efb\u52a1\u3002\u4f7f\u7528\u5b83\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5728 Python \u7a0b\u5e8f\u4e2d\u81ea\u52a8\u6267\u884c\u5404\u79cd\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u64cd\u4f5c\u3002<\/p>\n\n\n<style>.kb-image2692_f0465f-99 .kb-image-has-overlay:after{opacity:0.3;}.kb-image2692_f0465f-99 img.kb-img, .kb-image2692_f0465f-99 .kb-img img{box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}<\/style>\n<div class=\"wp-block-kadence-image kb-image2692_f0465f-99\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"600\" height=\"397\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-1-600x397.jpg\" alt=\"Python \ub514\ub809\ud1a0\ub9ac \uad6c\uc870 \ub2e4\ub8e8\uae30 \ud3ec\uc2a4\ud2b8 \uadf8\ub9bc2\" class=\"kb-img wp-image-2702\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-1-600x397.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-1-300x199.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-1-768x508.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-1.jpg 1200w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 os.listdir() \u83b7\u53d6\u76ee\u5f55\u5217\u8868<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757\u7684 <strong>listdir()<\/strong> \u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u4e00\u4e2a\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u5217\u8868\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5bfc\u5165 os\n\ndir_path = \"C:\/Users\/user\/Documents\" \u6587\u4ef6\u8def\u5f84\nfile_list = os.listdir(dir_path)\nprint(file_list)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u521b\u5efa\u6307\u5b9a\u7684\u76ee\u5f55\u8def\u5f84 (<kbd>C:\/Users\/user\/Documents<\/kbd>)\uff0c\u5b83\u4f1a\u8fd4\u56de\u8be5\u76ee\u5f55\u4e2d\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u7684\u5217\u8868\u3002\u56e0\u4e3a\u5b83\u53ea\u8fd4\u56de\u6587\u4ef6\u540d\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u901a\u8fc7\u540e\u7eed\u5904\u7406\u5bf9\u5b83\u4eec\u505a\u4efb\u4f55\u9700\u8981\u505a\u7684\u4e8b\u60c5\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0clistdir( ) \u8fd8\u5305\u62ec\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u9690\u85cf\u6587\u4ef6\u6216\u7cfb\u7edf\u6587\u4ef6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 os.path.join() \u5b89\u5168\u8fde\u63a5\u8def\u5f84<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5408\u5e76\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u8def\u5f84\u65f6\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u952e\u5165\u8def\u5f84\u5206\u9694\u7b26\u3001 <strong>os.path.join()<\/strong>\u53ef\u4ee5\u5b89\u5168\u4f7f\u7528\u3002\u8be5\u529f\u80fd\u5c06\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u4f7f\u7528\u9002\u5f53\u7684\u5206\u9694\u7b26\u81ea\u52a8\u751f\u6210\u8def\u5f84\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5bfc\u5165 os\n\nbase_path = \"C:\/Users\/user\/Documents\" \u57fa\u672c\u8def\u5f84\nfile_name = \"example.txt\"\nfull_path = os.path.join(base_path, file_name)\nprint(full_path)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u5c06\u8f93\u51fa\u6587\u4ef6\u6240\u5728\u8def\u5f84\u548c\u6587\u4ef6\u540d\/\u6269\u5c55\u540d\u3002\u7531\u4e8e\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4f7f\u7528\u4e0d\u540c\u7684\u8def\u5f84\u5206\u9694\u7b26\uff0c\u8fd9\u662f\u4f7f\u4ee3\u7801\u66f4\u52a0\u517c\u5bb9\u7684\u597d\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python \u76ee\u5f55\u7ed3\u6784\u5bfc\u822a\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u4e86\u80fd\u4ee5\u5217\u8868\u5f62\u5f0f\u83b7\u53d6\u76ee\u5f55\u5185\u7684\u6587\u4ef6\u5916\uff0c\u8fd8\u80fd\u4ee5\u5217\u8868\u5f62\u5f0f\u83b7\u53d6\u6587\u4ef6\u3001 <strong>\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757<\/strong>\u53ef\u4ee5\u8ba9\u4f60\u8f7b\u677e\u6d4f\u89c8\u591a\u4e2a\u76ee\u5f55\u548c\u5b50\u76ee\u5f55\u3002\u5373\u4f7f\u5728\u590d\u6742\u7684\u76ee\u5f55\u7ed3\u6784\u4e2d\uff0c\u4e5f\u80fd\u5feb\u901f\u627e\u5230\u60f3\u8981\u7684\u6587\u4ef6\u3002<\/p>\n\n\n<style>.kb-image2692_442fd8-66 .kb-image-has-overlay:after{opacity:0.3;}.kb-image2692_442fd8-66 img.kb-img, .kb-image2692_442fd8-66 .kb-img img{box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}<\/style>\n<div class=\"wp-block-kadence-image kb-image2692_442fd8-66\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"600\" height=\"492\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-2-600x492.jpg\" alt=\"Python \ub514\ub809\ud1a0\ub9ac \uad6c\uc870 \ub2e4\ub8e8\uae30 \ud3ec\uc2a4\ud2b8 \uadf8\ub9bc3\" class=\"kb-img wp-image-2703\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-2-600x492.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-2-300x246.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-2-768x630.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-2.jpg 1200w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 os.walk() \u6d4f\u89c8\u6240\u6709\u76ee\u5f55\u548c\u6587\u4ef6<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>os.walk()<\/strong>\u7528\u4e8e\u904d\u5386\u76ee\u5f55\u6811\u548c\u6d4f\u89c8\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002\u8be5\u51fd\u6570\u9ed8\u8ba4\u8fd4\u56de\u4e09\u4e2a\u503c\uff1a\u5f53\u524d\u76ee\u5f55\u7684\u8def\u5f84\u3001\u8be5\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u5939\u5217\u8868\u548c\u6587\u4ef6\u5217\u8868\u3002\u8bf7\u53c2\u89c1\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5bfc\u5165 os\n\nfor dirpath, dirnames, filenames in os.walk(\"C:\/Users\/user\/Documents\")\uff1a\n    print(f \"Current directory path: {dirpath}\")\n    print(f \"List of folders: {dirnames}\")\n    print(f \"List of files: {filenames}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u904d\u5386\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u5939\u548c\u6587\u4ef6\uff0c\u8ba9\u4f60\u4e00\u6b21\u6027\u4e86\u89e3\u6bcf\u4e2a\u76ee\u5f55\u7684\u7ed3\u6784\uff0c\u8fd9\u5728\u5904\u7406\u590d\u6742\u7684\u76ee\u5f55\u7ed3\u6784\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6309\u6587\u4ef6\u6269\u5c55\u540d\u7b5b\u9009<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4e00\u4e2a\u76ee\u5f55\u4e2d\u6709\u8bb8\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u65f6\uff0c\u4f60\u53ef\u80fd\u53ea\u60f3\u8fc7\u6ee4\u67d0\u4e9b\u6269\u5c55\u540d\u3002\u4f8b\u5982\uff0c\u5982\u679c\u53ea\u60f3\u5c06\".png \"\u6587\u4ef6\u653e\u5165\u5217\u8868\uff0c\u53ef\u4ee5\u8fd9\u6837\u7f16\u5199\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5bfc\u5165 os\n\ndir_path = \"C:\/Users\/user\/Documents\" (\u6587\u4ef6)\npng_files = [file for file in os.listdir(dir_path) if file.endswith(\".png\")\nprint(png_files)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u8ff0\u4ee3\u7801\u76f8\u5f53\u4e8e <strong>\u5217\u8868\u7406\u89e3<\/strong>\u4f7f\u7528 <code>.png<\/code>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u53ea\u9009\u62e9\u8981\u5904\u7406\u7684\u6587\u4ef6\u7c7b\u578b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757\u4e2d\u7684\u5b9e\u7528\u9644\u52a0\u529f\u80fd<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u4e86 <strong>\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757<\/strong>\u63d0\u4f9b\u4e86\u8bb8\u591a\u6709\u7528\u7684\u6587\u4ef6\u5904\u7406\u51fd\u6570\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5176\u4e2d\u4e00\u4e9b\u4e3b\u8981\u529f\u80fd\u3002<\/p>\n\n\n<style>.kb-image2692_ff4626-21 .kb-image-has-overlay:after{opacity:0.3;}.kb-image2692_ff4626-21 img.kb-img, .kb-image2692_ff4626-21 .kb-img img{box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}<\/style>\n<div class=\"wp-block-kadence-image kb-image2692_ff4626-21\"><figure class=\"aligncenter size-medium_large\"><img decoding=\"async\" width=\"768\" height=\"283\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-3-768x283.jpg\" alt=\"Python \ub514\ub809\ud1a0\ub9ac \uad6c\uc870 \ub2e4\ub8e8\uae30 \ud3ec\uc2a4\ud2b8 \uadf8\ub9bc4\" class=\"kb-img wp-image-2705\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-3-768x283.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-3-300x111.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-3-600x221.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-3.jpg 1200w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 os.mkdir() \u548c os.makedirs() \u521b\u5efa\u76ee\u5f55<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u60a8\u7ecf\u5e38\u9700\u8981\u521b\u5efa\u65b0\u76ee\u5f55\u3002 <strong>os.mkdir()<\/strong> \u51fd\u6570\u7528\u4e8e\u521b\u5efa\u5355\u4e2a\u76ee\u5f55\u3001 <strong>os.makedirs()<\/strong>\u53ef\u4ee5\u4e00\u6b21\u6027\u521b\u5efa\u6240\u6709\u5fc5\u8981\u7684\u76ee\u5f55\uff0c\u5305\u62ec\u7236\u76ee\u5f55\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5bfc\u5165 os\n\n# \u521b\u5efa\u5355\u4e2a\u76ee\u5f55\nos.mkdir(\"C:\/Users\/user\/Documents\/new_folder\")\n\n\u521b\u5efa\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\u7684 # \u5b50\u76ee\u5f55\nos.makedirs(\"C:\/Users\/user\/Documents\/new_folder\/sub_folder\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 os.remove() \u5220\u9664\u6587\u4ef6<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9700\u8981\u5220\u9664\u6587\u4ef6\u65f6 <strong>os.remove()<\/strong> \u51fd\u6570\u3002\u4e0d\u8fc7\uff0c\u8be5\u529f\u80fd\u53ea\u80fd\u5220\u9664\u6587\u4ef6\uff0c\u4e0d\u80fd\u5220\u9664\u76ee\u5f55\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5bfc\u5165 os\n\nfile_path = \"C:\/Users\/user\/Documents\/example.txt\"\nos.remove(file_path)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 os.rmdir() \u548c shutil.rmtree() \u5220\u9664\u76ee\u5f55<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5220\u9664\u76ee\u5f55 <strong>os.rmdir()<\/strong>\u4f46\u8be5\u51fd\u6570\u53ea\u80fd\u5220\u9664\u7a7a\u76ee\u5f55\u3002\u5982\u679c\u76ee\u5f55\u5185\u6709\u6587\u4ef6\uff0c\u5219 <strong>shutil.rmtree()<\/strong>\u5e94\u4f7f\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5bfc\u5165 os\n\u5bfc\u5165 shutil\n\n# \u5220\u9664\u4e00\u4e2a\u7a7a\u76ee\u5f55\nos.rmdir(\"C:\/Users\/user\/Documents\/new_folder\")\n\n\u5220\u9664\u5305\u542b # \u5b50\u6587\u4ef6\u7684\u76ee\u5f55\nshutil.rmtree(\"C:\/Users\/user\/Documents\/new_folder\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3\uff1a\u638c\u63e1 Python \u76ee\u5f55\u5bfc\u822a<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u60a8\u5b66\u4f1a\u4e86\u5982\u4f55\u5728 Python \u4e2d\u5bfc\u822a\u76ee\u5f55\u7ed3\u6784\u548c\u64cd\u4f5c\u6587\u4ef6\u3002 \u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u4ece\u5bfc\u822a\u76ee\u5f55\u5230\u521b\u5efa\u548c\u5220\u9664\u6587\u4ef6\u7684\u5404\u79cd\u4efb\u52a1\u3002\u8fd9\u4e9b\u57fa\u672c\u7684 Python \u76ee\u5f55\u5bfc\u822a\u6280\u80fd\u5c06\u5e2e\u52a9\u60a8\u5728\u672a\u6765\u5f00\u5c55\u66f4\u590d\u6742\u7684\u9879\u76ee\u3002<\/p>\n\n\n<style>.kb-image2692_0f2334-52 .kb-image-has-overlay:after{opacity:0.3;}<\/style>\n<div class=\"wp-block-kadence-image kb-image2692_0f2334-52\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"600\" height=\"443\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-4-600x443.jpg\" alt=\"Python \ub514\ub809\ud1a0\ub9ac \uad6c\uc870 \ub2e4\ub8e8\uae30 \ud3ec\uc2a4\ud2b8 \uadf8\ub9bc5\" class=\"kb-img wp-image-2706\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-4-600x443.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-4-300x221.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-4-768x566.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-7-4.jpg 1200w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u662f\u4e00\u4e2a\u5e0c\u671b\u9ad8\u6548\u4f7f\u7528 Python \u76ee\u5f55\u7ed3\u6784\u7684\u521d\u5b66\u8005\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u4f1a\u8ba9\u4f60\u66f4\u8f7b\u677e\u5730\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u4e86\u89e3\u4ee5\u4e0a\u5185\u5bb9\u540e\uff0c\u6211\u4eec\u80fd\u505a\u4e9b\u4ec0\u4e48\uff1f\u5982\u679c\u60a8\u60f3\u77e5\u9053 <a href=\"https:\/\/secondlife.lol\/zh\/converting-multiple-images-to-pdf-python\/\">\u8fd9\u91cc<\/a>\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\uff01<\/p>","protected":false},"excerpt":{"rendered":"<p>\ub9ce\uc740 \ucd08\ubcf4\uc790\ub4e4\uc774 Python\uc744 \ucc98\uc74c \ubc30\uc6b0\uba74\uc11c \ub514\ub809\ud1a0\ub9ac \uad6c\uc870\ub97c \uc5b4\ub5bb\uac8c \ud0d0\uc0c9\ud558\uace0 \ud30c\uc77c\uc744&#8230;<\/p>","protected":false},"author":3,"featured_media":2713,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[3],"tags":[283,288,284,289,287,286,285],"class_list":["post-2692","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-coding","tag-os-","tag-python--","tag-289","tag-287","tag-286","tag-285"],"taxonomy_info":{"category":[{"value":3,"label":"\ud30c\uc774\uc36c(Python)"}],"post_tag":[{"value":283,"label":"os \ubaa8\ub4c8"},{"value":288,"label":"Python \ub514\ub809\ud1a0\ub9ac \uad6c\uc870"},{"value":284,"label":"Python \ud30c\uc77c \ud0d0\uc0c9"},{"value":289,"label":"\ub514\ub809\ud1a0\ub9ac \uad6c\uc870 \ud0d0\uc0c9"},{"value":287,"label":"\ub514\ub809\ud1a0\ub9ac \ud0d0\uc0c9"},{"value":286,"label":"\ud30c\uc774\uc36c \uae30\ucd08"},{"value":285,"label":"\ud30c\uc77c \ucc98\ub9ac"}]},"featured_image_src_large":["https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/Python-\ub514\ub809\ud1a0\ub9ac-\uad6c\uc870-\ub2e4\ub8e8\uae30-\ud3ec\uc2a4\ud2b8-\uc378\ub124\uc77c-600x600.webp",600,600,true],"author_info":{"display_name":"TERE","author_link":"https:\/\/secondlife.lol\/zh\/author\/tere\/"},"comment_info":0,"category_info":[{"term_id":3,"name":"\ud30c\uc774\uc36c(Python)","slug":"python-coding","term_group":0,"term_taxonomy_id":3,"taxonomy":"category","description":"","parent":20,"count":119,"filter":"raw","cat_ID":3,"category_count":119,"category_description":"","cat_name":"\ud30c\uc774\uc36c(Python)","category_nicename":"python-coding","category_parent":20}],"tag_info":[{"term_id":283,"name":"os \ubaa8\ub4c8","slug":"os-%eb%aa%a8%eb%93%88","term_group":0,"term_taxonomy_id":283,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":288,"name":"Python \ub514\ub809\ud1a0\ub9ac \uad6c\uc870","slug":"python-%eb%94%94%eb%a0%89%ed%86%a0%eb%a6%ac-%ea%b5%ac%ec%a1%b0","term_group":0,"term_taxonomy_id":288,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":284,"name":"Python \ud30c\uc77c \ud0d0\uc0c9","slug":"python-%ed%8c%8c%ec%9d%bc-%ed%83%90%ec%83%89","term_group":0,"term_taxonomy_id":284,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":289,"name":"\ub514\ub809\ud1a0\ub9ac \uad6c\uc870 \ud0d0\uc0c9","slug":"%eb%94%94%eb%a0%89%ed%86%a0%eb%a6%ac-%ea%b5%ac%ec%a1%b0-%ed%83%90%ec%83%89","term_group":0,"term_taxonomy_id":289,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":287,"name":"\ub514\ub809\ud1a0\ub9ac \ud0d0\uc0c9","slug":"%eb%94%94%eb%a0%89%ed%86%a0%eb%a6%ac-%ed%83%90%ec%83%89","term_group":0,"term_taxonomy_id":287,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":286,"name":"\ud30c\uc774\uc36c \uae30\ucd08","slug":"%ed%8c%8c%ec%9d%b4%ec%8d%ac-%ea%b8%b0%ec%b4%88","term_group":0,"term_taxonomy_id":286,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":285,"name":"\ud30c\uc77c \ucc98\ub9ac","slug":"%ed%8c%8c%ec%9d%bc-%ec%b2%98%eb%a6%ac","term_group":0,"term_taxonomy_id":285,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"}],"_links":{"self":[{"href":"https:\/\/secondlife.lol\/zh\/wp-json\/wp\/v2\/posts\/2692","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/secondlife.lol\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/secondlife.lol\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/zh\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/zh\/wp-json\/wp\/v2\/comments?post=2692"}],"version-history":[{"count":3,"href":"https:\/\/secondlife.lol\/zh\/wp-json\/wp\/v2\/posts\/2692\/revisions"}],"predecessor-version":[{"id":2707,"href":"https:\/\/secondlife.lol\/zh\/wp-json\/wp\/v2\/posts\/2692\/revisions\/2707"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/zh\/wp-json\/wp\/v2\/media\/2713"}],"wp:attachment":[{"href":"https:\/\/secondlife.lol\/zh\/wp-json\/wp\/v2\/media?parent=2692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/secondlife.lol\/zh\/wp-json\/wp\/v2\/categories?post=2692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/secondlife.lol\/zh\/wp-json\/wp\/v2\/tags?post=2692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}