{"id":1481,"date":"2025-02-09T09:54:32","date_gmt":"2025-02-09T01:54:32","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1481"},"modified":"2025-02-09T09:54:33","modified_gmt":"2025-02-09T01:54:33","slug":"python-%e6%96%87%e4%bb%b6%e6%93%8d%e4%bd%9c%e4%b8%8e%e8%be%93%e5%85%a5%e8%be%93%e5%87%ba%ef%bc%9a%e8%af%bb%e5%86%99%e6%96%87%e4%bb%b6%e3%80%81%e7%9b%ae%e5%bd%95%e7%ae%a1%e7%90%86%e5%ae%8c%e5%85%a8","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/09\/python-%e6%96%87%e4%bb%b6%e6%93%8d%e4%bd%9c%e4%b8%8e%e8%be%93%e5%85%a5%e8%be%93%e5%87%ba%ef%bc%9a%e8%af%bb%e5%86%99%e6%96%87%e4%bb%b6%e3%80%81%e7%9b%ae%e5%bd%95%e7%ae%a1%e7%90%86%e5%ae%8c%e5%85%a8\/","title":{"rendered":"Python \u6587\u4ef6\u64cd\u4f5c\u4e0e\u8f93\u5165\u8f93\u51fa\uff1a\u8bfb\u5199\u6587\u4ef6\u3001\u76ee\u5f55\u7ba1\u7406\u5b8c\u5168\u6307\u5357"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u672c\u6559\u7a0b\uff0c\u60a8\u5c06\u638c\u63e1 Python \u4e2d\u7684\u6587\u4ef6\u64cd\u4f5c\u4e0e\u8f93\u5165\u8f93\u51fa\uff08I\/O\uff09\u6280\u5de7\u3002\u5b66\u4e60\u5982\u4f55\u6253\u5f00\u3001\u8bfb\u53d6\u3001\u5199\u5165\u6587\u4ef6\uff0c\u4f7f\u7528 <code>os<\/code> \u548c <code>shutil<\/code> \u6a21\u5757\u8fdb\u884c\u6587\u4ef6\u8def\u5f84\u7ba1\u7406\u3001\u6587\u4ef6\u590d\u5236\u4e0e\u79fb\u52a8\u3002\u7ed3\u5408\u5b9e\u4f8b\uff0c\u60a8\u5c06\u80fd\u8f7b\u677e\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\uff0c\u63d0\u5347\u9879\u76ee\u4e2d\u7684\u6587\u4ef6\u7ba1\u7406\u6548\u7387\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u8be6\u7ec6\u6559\u7a0b\u5185\u5bb9\uff1aPython \u6587\u4ef6\u64cd\u4f5c\u4e0e\u8f93\u5165\u8f93\u51fa<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u6587\u4ef6\u64cd\u4f5c<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6253\u5f00\u6587\u4ef6\uff1a<code>open()<\/code>\uff0c<code>with<\/code> \u8bed\u53e5<\/strong>\uff1a\u5728 Python \u4e2d\uff0c\u6587\u4ef6\u64cd\u4f5c\u901a\u5e38\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\uff0c<code>with<\/code> \u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u64cd\u4f5c\u5b8c\u6bd5\u540e\u6b63\u786e\u5173\u95ed\u6587\u4ef6\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  # \u4f7f\u7528 open() \u6253\u5f00\u6587\u4ef6\n  file = open(\"example.txt\", \"w\")  # \u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u5199\u5165\u64cd\u4f5c\n  file.write(\"Hello, Python!\")  # \u5199\u5165\u5185\u5bb9\n  file.close()  # \u5173\u95ed\u6587\u4ef6\n\n  # \u4f7f\u7528 with \u8bed\u53e5\u6253\u5f00\u6587\u4ef6\uff08\u63a8\u8350\uff09\n  with open(\"example.txt\", \"w\") as file:\n      file.write(\"Hello, Python with context manager!\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>open()<\/code> \u51fd\u6570\u7684\u5e38\u89c1\u6a21\u5f0f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"r\"<\/code>\uff1a\u53ea\u8bfb<\/li>\n\n\n\n<li><code>\"w\"<\/code>\uff1a\u5199\u5165\uff08\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u521b\u5efa\u6587\u4ef6\uff1b\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\uff09<\/li>\n\n\n\n<li><code>\"a\"<\/code>\uff1a\u8ffd\u52a0\uff08\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5185\u5bb9\uff09<\/li>\n\n\n\n<li><code>\"b\"<\/code>\uff1a\u4e8c\u8fdb\u5236\u6a21\u5f0f<\/li>\n\n\n\n<li><code>\"rb\"<\/code>\u3001<code>\"wb\"<\/code> \u7b49\uff1a\u4e8c\u8fdb\u5236\u6587\u4ef6\u64cd\u4f5c<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u6587\u4ef6\u8bfb\u53d6\uff1a<code>read()<\/code>\uff0c<code>readline()<\/code>\uff0c<code>readlines()<\/code><\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>read()<\/code><\/strong>\uff1a\u4e00\u6b21\u6027\u8bfb\u53d6\u6587\u4ef6\u7684\u5168\u90e8\u5185\u5bb9\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  with open(\"example.txt\", \"r\") as file:\n      content = file.read()\n      print(content)  # \u8f93\u51fa\u6587\u4ef6\u7684\u5168\u90e8\u5185\u5bb9<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>readline()<\/code><\/strong>\uff1a\u6bcf\u6b21\u8bfb\u53d6\u4e00\u884c\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  with open(\"example.txt\", \"r\") as file:\n      line = file.readline()\n      while line:\n          print(line.strip())  # \u53bb\u9664\u6362\u884c\u7b26\n          line = file.readline()<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>readlines()<\/code><\/strong>\uff1a\u5c06\u6587\u4ef6\u7684\u6240\u6709\u884c\u4f5c\u4e3a\u5217\u8868\u8fd4\u56de\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  with open(\"example.txt\", \"r\") as file:\n      lines = file.readlines()\n      for line in lines:\n          print(line.strip())  # \u53bb\u9664\u6362\u884c\u7b26<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u6587\u4ef6\u5199\u5165\uff1a<code>write()<\/code>\uff0c<code>writelines()<\/code><\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>write()<\/code><\/strong>\uff1a\u5c06\u6307\u5b9a\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  with open(\"example.txt\", \"w\") as file:\n      file.write(\"This is a line of text.\\n\")<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>writelines()<\/code><\/strong>\uff1a\u5c06\u4e00\u4e2a\u5e8f\u5217\u7684\u5185\u5bb9\u5199\u5165\u6587\u4ef6\uff08\u5982\u5217\u8868\uff09\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  lines = &#91;\"Line 1\\n\", \"Line 2\\n\", \"Line 3\\n\"]\n  with open(\"example.txt\", \"w\") as file:\n      file.writelines(lines)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u6587\u4ef6\u5173\u95ed\u4e0e\u6587\u4ef6\u6307\u9488\u4f4d\u7f6e<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u6587\u4ef6\u64cd\u4f5c\u7ed3\u675f\u540e\uff0c\u8c03\u7528 <code>close()<\/code> \u5173\u95ed\u6587\u4ef6\uff0c\u91ca\u653e\u7cfb\u7edf\u8d44\u6e90\u3002\u5982\u679c\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\uff0c\u5219\u4f1a\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u6587\u4ef6\u6307\u9488<\/strong>\uff1a\u6bcf\u6b21\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u6587\u4ef6\u6307\u9488\u4f1a\u5411\u4e0b\u79fb\u52a8\uff0c\u8bfb\u53d6\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u7684\u5185\u5bb9\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  with open(\"example.txt\", \"r\") as file:\n      print(file.tell())  # \u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u6307\u9488\u7684\u4f4d\u7f6e\n      content = file.read(5)  # \u8bfb\u53d6 5 \u4e2a\u5b57\u7b26\n      print(file.tell())  # \u518d\u6b21\u83b7\u53d6\u6587\u4ef6\u6307\u9488\u7684\u4f4d\u7f6e<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>os<\/code> \u6a21\u5757<\/strong>\uff1a\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3001\u521b\u5efa\u3001\u5220\u9664\u3001\u91cd\u547d\u540d\u6587\u4ef6\u548c\u76ee\u5f55\u7b49\u64cd\u4f5c\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import os\n\n  # \u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\n  print(os.getcwd())  # \u8f93\u51fa\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\n\n  # \u521b\u5efa\u76ee\u5f55\n  os.mkdir(\"new_directory\")  # \u521b\u5efa\u65b0\u76ee\u5f55\n\n  # \u5220\u9664\u6587\u4ef6\n  os.remove(\"example.txt\")  # \u5220\u9664\u6587\u4ef6\n\n  # \u91cd\u547d\u540d\u6587\u4ef6\n  os.rename(\"old_name.txt\", \"new_name.txt\")  # \u91cd\u547d\u540d\u6587\u4ef6\n\n  # \u5217\u51fa\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939\n  print(os.listdir(\".\"))  # \u8f93\u51fa\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>shutil<\/code> \u6a21\u5757<\/strong>\uff1a\u63d0\u4f9b\u4e86\u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c\uff0c\u5982\u590d\u5236\u3001\u79fb\u52a8\u3001\u5220\u9664\u6587\u4ef6\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import shutil\n\n  # \u590d\u5236\u6587\u4ef6\n  shutil.copy(\"source.txt\", \"destination.txt\")  # \u590d\u5236\u6587\u4ef6\n\n  # \u590d\u5236\u76ee\u5f55\u53ca\u5176\u5185\u5bb9\n  shutil.copytree(\"source_directory\", \"destination_directory\")  # \u590d\u5236\u76ee\u5f55\n\n  # \u79fb\u52a8\u6587\u4ef6\n  shutil.move(\"source.txt\", \"new_location.txt\")  # \u79fb\u52a8\u6587\u4ef6\n\n  # \u5220\u9664\u76ee\u5f55\u53ca\u5176\u5185\u5bb9\n  shutil.rmtree(\"directory_to_remove\")  # \u5220\u9664\u76ee\u5f55\u53ca\u5176\u6240\u6709\u5185\u5bb9<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u672c\u6559\u7a0b\uff0c\u60a8\u5c06\u638c\u63e1 Python \u4e2d\u6587\u4ef6\u64cd\u4f5c\u7684\u57fa\u7840\u4e0e\u8fdb\u9636\u6280\u5de7\uff0c\u5b66\u4f1a\u5982\u4f55\u6253\u5f00\u3001\u8bfb\u53d6\u3001\u5199\u5165\u6587\u4ef6\uff0c\u5e76\u6709\u6548\u5730\u7ba1\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 <code>os<\/code> \u548c <code>shutil<\/code> \u6a21\u5757\u6765\u8fdb\u884c\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\u3001\u590d\u5236\u3001\u79fb\u52a8\u7b49\u9ad8\u7ea7\u6587\u4ef6\u7ba1\u7406\u4efb\u52a1\u3002\u8fd9\u4e9b\u6280\u80fd\u5bf9\u4e8e\u5904\u7406\u9879\u76ee\u4e2d\u7684\u6570\u636e\u6587\u4ef6\u548c\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u81f3\u5173\u91cd\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7\u672c\u6559\u7a0b\uff0c\u60a8\u5c06\u638c\u63e1 Python \u4e2d\u7684\u6587\u4ef6\u64cd\u4f5c\u4e0e\u8f93\u5165\u8f93\u51fa\uff08I\/O\uff09\u6280\u5de7\u3002\u5b66\u4e60\u5982\u4f55\u6253\u5f00\u3001\u8bfb\u53d6\u3001\u5199\u5165\u6587\u4ef6\uff0c\u4f7f\u7528  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-1481","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1481"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1481\/revisions"}],"predecessor-version":[{"id":1482,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1481\/revisions\/1482"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}