{"id":152207,"date":"2019-08-09T09:06:15","date_gmt":"2019-08-09T01:06:15","guid":{"rendered":"https:\/\/gulass.cn\/?p=152207"},"modified":"2019-07-28T15:07:08","modified_gmt":"2019-07-28T07:07:08","slug":"python-read-write","status":"publish","type":"post","link":"https:\/\/gulass.cn\/python-read-write.html","title":{"rendered":"python\u8bfb\u5199\u6587\u4ef6"},"content":{"rendered":"
\u5728\u6587\u4ef6\u5939\u5c42\u6b21\u7ed3\u6784\u4e2d\u7684\u4e00\u4e2a\u6587\u4ef6<\/p>\n
\u5728 Windows \u4e0a\uff0c\u8def\u5f84\u4e66\u5199\u4f7f\u7528\u5012\u659c\u6760\u4f5c\u4e3a\u6587\u4ef6\u5939\u4e4b\u95f4\u7684\u5206\u9694\u7b26\u3002\u4f46\u5728 OS X \u548cLinux \u4e0a\uff0c\u4f7f\u7528\u6b63\u659c\u6760\u4f5c\u4e3a\u5b83\u4eec\u7684\u8def\u5f84\u5206\u9694\u7b26\u3002\u5982\u679c\u60f3\u8981\u7a0b\u5e8f\u8fd0\u884c\u5728\u6240\u6709\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u5728\u7f16\u5199 Python \u811a\u672c\u65f6\uff0c\u5c31\u5fc5\u987b\u5904\u7406\u8fd9\u4e24\u79cd\u60c5\u51b5\u3002<\/p>\n
\u5982\u679c\u5c06\u5355\u4e2a\u6587\u4ef6\u548c\u8def\u5f84\u4e0a\u7684\u6587\u4ef6\u5939\u540d\u79f0\u7684\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9\u5b83\uff0cos.path.join()\u5c31\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u7684\u5b57\u7b26\u4e32\uff0c\u5305\u542b\u6b63 \u786e\u7684\u8def\u5f84\u5206\u9694\u7b26\u3002<\/p>\n
>><\/span>> import os\r\n>><\/span>> os.path.join('usr'<\/span>,'bin'<\/span>,'spam'<\/span>)\r\n'usr\\\\bin\\\\spam'<\/span>\r\n<\/code><\/pre>\n\u5982\u679c\u9700\u8981\u521b\u5efa\u6587\u4ef6\u540d\u79f0\u7684\u5b57\u7b26\u4e32\uff0cos.path.join()\u51fd\u6570\u5c31\u5f88\u6709\u7528\u3002\u8fd9\u4e9b\u5b57\u7b26\u4e32\u5c06\u4f20\u9012\u7ed9\u51e0\u4e2a\u6587\u4ef6\u76f8\u5173\u7684\u51fd\u6570\u3002<\/p>\n
\u4e0b\u9762\u7684\u4f8b\u5b50\u5c06\u4e00\u4e2a\u6587\u4ef6\u540d\u5217\u8868\u4e2d\u7684\u540d\u79f0\uff0c\u6dfb\u52a0\u5230\u6587\u4ef6\u5939\u540d\u79f0\u7684\u672b\u5c3e\u3002<\/p>\n
>>> myFiles = ['accounts.txt'<\/span>, 'details.csv'<\/span>, 'invite.docx'<\/span>]\r\n>>> for<\/span> filename in<\/span> myFiles:\r\nprint<\/span>(os.path.join('C:\\\\Users\\\\asweigart'<\/span>, filename)<\/span><\/span>)\r\nC:\\Users\\asweigart\\accounts.txt<\/span>\r\nC:\\Users\\asweigart\\details<\/span>.csv<\/span>\r\nC:\\Users\\asweigart\\invite.docx<\/span>\r\n<\/code><\/pre>\n\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/span><\/div>\n\u6bcf\u4e2a\u8fd0\u884c\u5728\u8ba1\u7b97\u673a\u4e0a\u7684\u7a0b\u5e8f\uff0c\u90fd\u6709\u4e00\u4e2a\u201c\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u201d\uff0c\u6216cwd\u3002\u6240\u6709\u6ca1\u6709\u4ece\u6839\u6587\u4ef6\u5939\u5f00\u59cb\u7684\u6587\u4ef6\u540d\u6216\u8def\u5f84\uff0c\u90fd\u5047\u5b9a\u5728\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e0b\u3002\u5229\u7528os.getcwd()\u51fd\u6570\uff0c\u53ef\u4ee5\u53d6\u5f97\u5f53\u524d\u5de5\u4f5c\u8def\u5f84\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u53ef\u4ee5\u5229\u7528 os.chdir()\u6539\u53d8\u5b83\u3002<\/em><\/p>\n\u7edd\u5bf9\u8def\u5f84\u4e0e\u76f8\u5bf9\u8def\u5f84<\/strong><\/span><\/div>\n\n- \u201c\u7edd\u5bf9\u8def\u5f84\u201d\uff0c\u603b\u662f\u4ece\u6839\u6587\u4ef6\u5939\u5f00\u59cb\u3002<\/li>\n
- \u201c\u76f8\u5bf9\u8def\u5f84\u201d\uff0c\u5b83\u76f8\u5bf9\u4e8e\u7a0b\u5e8f\u7684\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002<\/li>\n<\/ul>\n
\u8fd8\u6709\u70b9\uff08.\uff09\u548c\u70b9\u70b9\uff08..\uff09\u6587\u4ef6\u5939\u3002\u5b83\u4eec\u4e0d\u662f\u771f\u6b63\u7684\u6587\u4ef6\u5939\uff0c\u800c\u662f\u53ef\u4ee5\u5728\u8def\u5f84\u4e2d\u4f7f\u7528\u7684\u7279\u6b8a\u540d\u79f0*\u3002\u5355\u4e2a\u7684\u53e5\u70b9\uff08\u201c\u70b9\u201d\uff09\u7528\u4f5c\u6587\u4ef6\u5939\u76ee\u540d\u79f0\u65f6\uff0c\u662f\u201c\u8fd9\u4e2a\u76ee\u5f55\u201d\u7684\u7f29\u5199\u3002\u4e24\u4e2a\u53e5\u70b9\uff08\u201c\u70b9\u70b9\u201d\uff09\u610f\u601d\u662f\u7236\u6587\u4ef6\u5939\u3002*<\/p>\n
\u5728\u5de5\u4f5c\u76ee\u5f55 C:\\bacon \u4e2d\u7684\u6587\u4ef6\u5939\u548c\u6587\u4ef6\u7684\u76f8\u5bf9\u8def\u5f84\uff1a<\/p>\n
\u7528 os.makedirs()\u521b\u5efa\u65b0\u6587\u4ef6\u5939<\/strong><\/span><\/div>\n\u5904\u7406\u7edd\u5bf9\u8def\u5f84\u548c\u76f8\u5bf9\u8def\u5f84<\/strong><\/span><\/div>\n\n- os.path \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u76f8\u5bf9\u8def\u5f84\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u4ee5\u53ca\u68c0\u67e5\u7ed9\u5b9a\u7684\u8def\u5f84\u662f\u5426\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002<\/li>\n
- \u8c03\u7528 os.path.abspath(path)\u5c06\u8fd4\u56de\u53c2\u6570\u7684\u7edd\u5bf9\u8def\u5f84\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u662f\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u7684\u7b80\u4fbf\u65b9\u6cd5\u3002<\/li>\n
- \u8c03\u7528 os.path.isabs(path)\uff0c\u5982\u679c\u53c2\u6570\u662f\u4e00\u4e2a\u7edd\u5bf9\u8def\u5f84\uff0c\u5c31\u8fd4\u56deTrue\uff0c\u5982\u679c\u53c2\u6570\u662f\u4e00\u4e2a\u76f8\u5bf9\u8def\u5f84\uff0c\u5c31\u8fd4\u56de False\u3002<\/li>\n
- \u8c03\u7528 os.path.relpath(path,start)\u5c06\u8fd4\u56de\u4ecestart\u8def\u5f84\u5230path\u7684\u76f8\u5bf9\u8def\u5f84\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u6ca1\u6709\u63d0\u4f9b start\uff0c\u5c31\u4f7f\u7528\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4f5c\u4e3a\u5f00\u59cb\u8def\u5f84\u3002<\/li>\n
- \u8c03\u7528 os.path.dirname(path)\u5c06\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5b83\u5305\u542bpath\u53c2\u6570\u4e2d\u6700\u540e\u4e00\u4e2a\u659c\u6760 \u4e4b\u524d\u7684\u6240\u6709\u5185\u5bb9\u3002<\/li>\n
- \u8c03\u7528 os.path.basename(path)\u5c06\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5b83\u5305\u542bpath\u53c2\u6570\u4e2d\u6700\u540e\u4e00\u4e2a\u659c\u6760\u4e4b\u540e\u7684\u6240\u6709\u5185\u5bb9\u3002<\/li>\n
- \u5982\u679c\u540c\u65f6\u9700\u8981\u4e00\u4e2a\u8def\u5f84\u7684\u76ee\u5f55\u540d\u79f0\u548c\u57fa\u672c\u540d\u79f0\uff0c\u5c31\u53ef\u4ee5\u8c03\u7528os.path.split()\uff0c\u83b7\u5f97\u8fd9\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5143\u7ec4<\/li>\n<\/ul>\n
>><\/span>> calcFilePath = 'C:\\\\Windows\\\\System32\\\\calc.exe'<\/span>\r\n>><\/span>> os.path.split(calcFilePath)\r\n('C:\\\\Windows\\\\System32'<\/span>, 'calc.exe'<\/span>)\r\n<\/code><\/pre>\nos.path.split()\u4e0d\u4f1a\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u5e76\u8fd4\u56de\u6bcf\u4e2a\u6587\u4ef6\u5939\u7684\u5b57\u7b26\u4e32\u7684\u5217\u8868<\/em>\u3002\u5982\u679c\u9700\u8981\u8fd9\u6837\uff0c\u8bf7\u4f7f\u7528split()\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u5e76\u6839\u636eos.path.sep\u4e2d\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u5206\u5272\u3002\u56de\u5fc6\u4e00\u4e0b\uff0c\u6839\u636e\u7a0b\u5e8f\u8fd0\u884c\u7684\u8ba1\u7b97\u673a\uff0cos.path.sep \u53d8\u91cf\u8bbe\u7f6e\u4e3a\u6b63\u786e\u7684\u6587\u4ef6\u5939\u5206\u5272\u659c\u6760\u3002<\/p>\n>>> calcFilePath.split(os.path<\/span>.sep<\/span>)\r\n['C:'<\/span>, 'Windows'<\/span>, 'System32'<\/span>, 'calc.exe'<\/span>]\r\n<\/code><\/pre>\n\u67e5\u770b\u6587\u4ef6\u5927\u5c0f\u548c\u6587\u4ef6\u5939\u5185\u5bb9<\/strong><\/span><\/div>\n\n- \u8c03\u7528 os.path.getsize(path)\u5c06\u8fd4\u56de path \u53c2\u6570\u4e2d\u6587\u4ef6\u7684\u5b57\u8282\u6570\u3002<\/li>\n
- \u8c03\u7528 os.listdir(path)\u5c06\u8fd4\u56de\u6587\u4ef6\u540d\u5b57\u7b26\u4e32\u7684\u5217\u8868\uff0c\u5305\u542b path \u53c2\u6570\u4e2d\u7684\u6bcf\u4e2a\u6587\u4ef6<\/li>\n<\/ul>\n\u68c0\u67e5\u8def\u5f84\u6709\u6548\u6027<\/strong><\/span><\/div>\n
\n- \u5982\u679c path \u53c2\u6570\u6240\u6307\u7684\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u5b58\u5728\uff0c\u8c03\u7528 os.path.exists(path)\u5c06\u8fd4\u56de True\uff0c\u5426\u5219\u8fd4\u56de False\u3002<\/li>\n
- \u5982\u679c path \u53c2\u6570\u5b58\u5728\uff0c\u5e76\u4e14\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u8c03\u7528 os.path.isfile(path)\u5c06\u8fd4\u56de True\uff0c\u5426\u5219\u8fd4\u56de False\u3002<\/li>\n<\/ul>\n\u6587\u4ef6\u8bfb\u5199\u8fc7\u7a0b<\/strong><\/span><\/div>\n
\u5728 Python \u4e2d\uff0c\u8bfb\u5199\u6587\u4ef6\u6709 3 \u4e2a\u6b65\u9aa4\uff1a<\/p>\n
\n- \u8c03\u7528 open()\u51fd\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a File \u5bf9\u8c61\u3002<\/li>\n
- \u8c03\u7528 File \u5bf9\u8c61\u7684 read()\u6216 write()\u65b9\u6cd5\u3002<\/li>\n
- \u8c03\u7528 File \u5bf9\u8c61\u7684 close()\u65b9\u6cd5\uff0c\u5173\u95ed\u8be5\u6587\u4ef6\u3002<\/li>\n<\/ol>\n\u7528 open()\u51fd\u6570\u6253\u5f00\u6587\u4ef6<\/strong><\/span><\/div>\n
\u8981\u7528 open()\u51fd\u6570\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5c31\u8981\u5411\u5b83\u4f20\u9012\u4e00\u4e2a\u5b57\u7b26\u4e32\u8def\u5f84<\/em>\uff0c\u8868\u660e\u5e0c\u671b\u6253\u5f00\u7684\u6587\u4ef6\u3002\u8fd9\u65e2\u53ef\u4ee5\u662f\u7edd\u5bf9\u8def\u5f84\uff0c\u4e5f\u53ef\u4ee5\u662f\u76f8\u5bf9\u8def\u5f84<\/em>\u3002open()\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a File \u5bf9\u8c61\u3002<\/p>\n\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/span><\/div>\n\n- \u8c03\u7528 open()\u5c06\u8fd4\u56de\u4e00\u4e2aFile\u5bf9\u8c61\u3002File\u5bf9\u8c61\u4ee3\u8868\u8ba1\u7b97\u673a\u4e2d\u7684\u4e00\u4e2a\u6587\u4ef6\uff0c\u5b83\u53ea\u662fPython \u4e2d\u53e6\u4e00\u79cd\u7c7b\u578b\u7684\u503c\uff0c\u5c31\u50cf\u4f60\u5df2\u719f\u6089\u7684\u5217\u8868\u548c\u5b57\u5178\u3002<\/li>\n
- \u8c03\u7528readlines()\u65b9\u6cd5\uff0c\u4ece\u6587\u4ef6\u53d6\u5f97\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u5217\u8868\u3002\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u5c31\u662f\u6587\u672c\u4e2d\u7684\u6bcf\u4e00\u884c<\/li>\n
- \u8c03\u7528read()\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6587\u4ef6\u7684\u5185\u5bb9\u8bfb\u53d6\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u503c\u3002\u5982\u679c\u5c06\u6587\u4ef6\u7684\u5185\u5bb9\u770b\u6210\u662f\u5355\u4e2a\u5927\u5b57\u7b26\u4e32\uff0c\u7528\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u5f88\u597d\u3002<\/li>\n
- \u4e0e\u5355\u4e2a\u5927\u5b57\u7b26\u4e32\u76f8\u6bd4\uff0c\u5b57\u7b26\u4e32\u7684\u5217\u8868\u901a\u66f4\u6613\u5904\u7406<\/li>\n<\/ul>\n\u5199\u5165\u6587\u4ef6<\/strong><\/span><\/div>\n
\n- \u5982\u679c\u6253\u5f00\u6587\u4ef6\u65f6\u7528\u8bfb\u6a21\u5f0f\uff0c\u5c31\u4e0d\u80fd\u5199\u5165\u6587\u4ef6\u3002\u4f60\u9700\u8981\u4ee5\u201c\u5199\u5165\u7eaf\u6587\u672c\u6a21\u5f0f\u201d\u6216\u201c\u6dfb\u52a0\u7eaf\u6587\u672c\u6a21\u5f0f\u201d\u6253\u5f00\u8be5\u6587\u4ef6\uff0c\u6216\u7b80\u79f0\u4e3a\u201c\u5199\u6a21\u5f0f\u201d\u548c\u201c\u6dfb\u52a0\u6a21\u5f0f\u201d\u3002<\/li>\n
- \u5199\u6a21\u5f0f\u5c06\u8986\u5199\u539f\u6709\u7684\u6587\u4ef6\uff0c\u4ece\u5934\u5f00\u59cb\uff0c\u5c31\u50cf\u4f60\u7528\u4e00\u4e2a\u65b0\u503c\u8986\u5199\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u3002\u5c06'w'\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9 open()\uff0c\u4ee5\u5199\u6a21\u5f0f\u6253\u5f00\u8be5\u6587\u4ef6\u3002<\/li>\n
- \u6dfb\u52a0\u6a21\u5f0f\u5c06\u5728\u5df2\u6709\u6587\u4ef6\u7684\u672b\u5c3e\u6dfb\u52a0\u6587\u672c\u3002\u4f60\u53ef\u4ee5\u8ba4\u4e3a\u8fd9\u7c7b\u4f3c\u5411\u4e00\u4e2a\u53d8\u91cf\u4e2d\u7684\u5217\u8868\u6dfb\u52a0\u5185\u5bb9\uff0c\u800c\u4e0d\u662f\u5b8c\u5168\u8986\u5199\u8be5\u53d8\u91cf\u3002\u5c06'a'\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9open()\uff0c\u4ee5\u6dfb\u52a0\u6a21\u5f0f\u6253\u5f00\u8be5\u6587\u4ef6\u3002<\/li>\n
- \u5982\u679c\u4f20\u9012\u7ed9 open()\u7684\u6587\u4ef6\u540d\u4e0d\u5b58\u5728\uff0c\u5199\u6a21\u5f0f\u548c\u6dfb\u52a0\u6a21\u5f0f\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7a7a\u6587\u4ef6\u3002<\/li>\n
- \u5728\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u540e\uff0c\u8c03\u7528 close()\u65b9\u6cd5\uff0c\u7136\u540e\u624d\u80fd\u518d\u6b21\u6253\u5f00\u8be5\u6587\u4ef6<\/em>\u3002<\/li>\n<\/ul>\n
>><\/span>> baconFile = open('bacon.txt'<\/span>, 'w'<\/span>)\r\n>><\/span>> baconFile.write('Hello world!\\n'<\/span>)\r\n13<\/span>\r\n>><\/span>> baconFile.close()\r\n>><\/span>> baconFile = open('bacon.txt'<\/span>, 'a'<\/span>)\r\n>><\/span>> baconFile.write('Bacon is not a vegetable.'<\/span>)\r\n25<\/span>\r\n>><\/span>> baconFile.close()\r\n>><\/span>> baconFile = open('bacon.txt'<\/span>)\r\n>><\/span>> content = baconFile.read()\r\n>><\/span>> baconFile.close()\r\n>><\/span>> print(content)\r\nHello world!\r\nBacon is not<\/span> a vegetable.\r\n<\/code><\/pre>\nwrite()\u65b9\u6cd5\u4e0d\u4f1a\u50cf print()\u51fd\u6570\u90a3\u6837\uff0c\u5728\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u81ea\u52a8\u6dfb\u52a0\u6362\u884c\u5b57\u7b26\u3002\u5fc5\u987b\u81ea\u5df1\u6dfb\u52a0\u8be5\u5b57\u7b26\u3002<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Windows \u4e2d\uff0c\u6839\u6587\u4ef6\u5939\u540d\u4e3a C:\\\uff0c\u4e5f\u79f0\u4e3a C\uff1a\u76d8\u3002\u5728 OS X \u548c Linux\u4e2d\uff0c\u6839\u6587\u4ef6\u5939\u662f\/ \u6587 […]<\/p>\n","protected":false},"author":1476,"featured_media":113018,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-152207","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/152207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1476"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=152207"}],"version-history":[{"count":7,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/152207\/revisions"}],"predecessor-version":[{"id":152220,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/152207\/revisions\/152220"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/113018"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=152207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=152207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=152207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}