tempfile\u5e93\u7684\u4f7f\u7528<\/strong><\/div>\ntempfile\u4e00\u822c\u662fpython\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\u5e93\uff0c\u4e0d\u9700\u8981\u5355\u72ec\u5b89\u88c5\uff0c\u8fd9\u91cc\u6211\u4eec\u76f4\u63a5\u4ecb\u7ecd\u4e00\u4e0b\u5176\u5e38\u89c4\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n
\r\n# tempfile_test.py\r\n \r\nimport tempfile\r\n \r\nfile = tempfile.NamedTemporaryFile()\r\nname = str(file.name)\r\nfile.write('This is the first tmp file!'.encode('utf-8'))\r\nfile.close()\r\n \r\nprint (name)<\/pre>\n\u4e0a\u8ff0\u4ee3\u7801\u6267\u884c\u7684\u4efb\u52a1\u4e3a\uff1a\u4f7f\u7528tempfile.NamedTemporaryFile\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6587\u4ef6\uff0c\u5176\u6587\u4ef6\u540d\u91c7\u7528\u7684\u662f\u968f\u673a\u5316\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u4f5c\u4e3aname\u8fd9\u6837\u7684\u4e00\u4e2a\u5c5e\u6027\u6765\u8c03\u7528\u3002\u901a\u8fc7\u6267\u884c\u8fd9\u4e2a\u4efb\u52a1\uff0c\u6211\u4eec\u53ef\u4ee5\u67e5\u770b\u4e00\u822c\u662f\u751f\u6210\u4ec0\u4e48\u6837\u683c\u5f0f\u7684\u4e34\u65f6\u6587\u4ef6\uff1a<\/p>\n
\r\n[dechin@dechin-manjaro tmp_file]$ python3 tempfile_test.py \r\n\/tmp\/tmppetcksa8\r\n[dechin@dechin-manjaro tmp_file]$ ll\r\n\u603b\u7528\u91cf 4\r\n-rw-r--r-- 1 dechin dechin 181 1\u6708 27 21:39 tempfile_test.py\r\n[dechin@dechin-manjaro tmp_file]$ cat \/tmp\/tmppetcksa8\r\ncat: \/tmp\/tmppetcksa8: \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55<\/pre>\n\u5728\u8fd9\u4e2apython\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u4ea7\u751f\u4e86tmppetcksa8\u8fd9\u6837\u7684\u4e00\u4e2a\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u5411\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u76f4\u63a5write\u4e00\u4e9b\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2a\u4e34\u65f6\u6587\u4ef6\u88ab\u5b58\u50a8\u5728tmp\u76ee\u5f55\u4e0b\uff0c\u4e0e\u5f53\u524d\u7684\u6267\u884c\u8def\u5f84\u65e0\u5173\u3002\u540c\u65f6\u6267\u884c\u7ed3\u675f\u4e4b\u540e\u6211\u4eec\u53d1\u73b0\uff0c\u4ea7\u751f\u7684\u8fd9\u4e2a\u4e34\u65f6\u6587\u4ef6\u88ab\u5220\u9664\u4e86\uff0c\u8fd9\u662fNamedTemporaryFile\u81ea\u5e26\u7684\u4e00\u4e2adelete\u7684\u5c5e\u6027\uff0c\u9ed8\u8ba4\u914d\u7f6e\u662f\u5173\u95ed\u4e34\u65f6\u6587\u4ef6\u540e\u76f4\u63a5\u5220\u9664\u3002<\/p>\n
\u6301\u4e45\u5316\u4fdd\u5b58\u4e34\u65f6\u6587\u4ef6<\/strong><\/div>\n\u9700\u8981\u6301\u4e45\u5316\u4fdd\u5b58\u4e34\u65f6\u6587\u4ef6\u662f\u975e\u5e38\u5bb9\u6613\u7684\uff0c\u53ea\u9700\u8981\u5c06\u4e0a\u8ff0\u7ae0\u8282\u4e2d\u7684delete\u5c5e\u6027\u8bbe\u7f6e\u4e3aFalse\u5373\u53ef\uff1a<\/p>\n
\r\n# tempfile_test.py\r\n \r\nimport tempfile\r\n \r\nfile = tempfile.NamedTemporaryFile(delete=False)\r\nname = str(file.name)\r\nfile.write('This is the first tmp file!'.encode('utf-8'))\r\nfile.close()\r\n \r\nprint (name)<\/pre>\n\u8fd9\u91cc\u6211\u4eec\u552f\u4e00\u7684\u53d8\u52a8\uff0c\u53ea\u662f\u5728\u62ec\u53f7\u4e2d\u52a0\u4e0a\u4e86delete=True\u8fd9\u4e00\u8bbe\u5b9a\uff0c\u8fd9\u4e2a\u8bbe\u5b9a\u53ef\u4ee5\u5141\u8bb8\u6211\u4eec\u6301\u4e45\u5316\u7684\u5b58\u50a8\u4e34\u65f6\u6587\u4ef6\uff1a<\/p>\n
\r\n[dechin@dechin-manjaro tmp_file]$ python3 tempfile_test.py \r\n\/tmp\/tmpwlt27ryk\r\n[dechin@dechin-manjaro tmp_file]$ cat \/tmp\/tmpwlt27ryk\r\nThis is the first tmp file!<\/pre>\n\u8bbe\u7f6e\u4e34\u65f6\u6587\u4ef6\u540e\u7f00<\/strong><\/div>\n\u5728\u6709\u4e9b\u573a\u666f\u4e0b\u5bf9\u4e8e\u4e34\u65f6\u6587\u4ef6\u7684\u5b58\u50a8\u6709\u4e00\u5b9a\u7684\u683c\u5f0f\u8981\u6c42\uff0c\u6bd4\u5982\u540e\u7f00\u7b49\uff0c\u8fd9\u91cc\u6211\u4eec\u5c06\u4e34\u65f6\u6587\u4ef6\u7684\u540e\u7f00\u8bbe\u7f6e\u4e3a\u5e38\u7528\u7684txt\u683c\u5f0f\uff0c\u540c\u6837\u7684\uff0c\u53ea\u9700\u8981\u5728NamedTemporaryFile\u7684\u53c2\u6570\u4e2d\u8fdb\u884c\u914d\u7f6e\u5373\u53ef\uff1a<\/p>\n
\r\n# tempfile_test.py\r\n \r\nimport tempfile\r\n \r\nfile = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')\r\nname = str(file.name)\r\nfile.write('This is the first tmp file!'.encode('utf-8'))\r\nfile.close()\r\n \r\nprint (name)<\/pre>\n\u7531\u4e8e\u8fd8\u662f\u8bbe\u7f6e\u4e86delete=True\u53c2\u6570\uff0c\u56e0\u6b64\u8be5\u4e34\u65f6txt\u6587\u4ef6\u88ab\u6301\u4e45\u5316\u7684\u4fdd\u5b58\u5728\u7cfb\u7edf\u4e2d\u7684\/tmp\u76ee\u5f55\u4e0b\uff1a<\/p>\n
\r\n[dechin@dechin-manjaro tmp_file]$ python3 tempfile_test.py \r\n\/tmp\/tmpk0ct_kzs.txt\r\n[dechin@dechin-manjaro tmp_file]$ cat \/tmp\/tmpk0ct_kzs.txt\r\nThis is the first tmp file!<\/pre>\n\u603b\u7ed3\u6982\u8981<\/strong><\/div>\n\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u4e86python\u4e2d\u81ea\u5e26\u7684tempfile\u5e93\u5bf9\u4e34\u65f6\u6587\u4ef6\u7684\u64cd\u4f5c\uff0c\u901a\u8fc7tempfile\u5e93\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u81ea\u52a8\u5220\u9664\u7684\u6216\u8005\u6301\u4e45\u5316\u5b58\u50a8\u7684\u4e34\u65f6\u6587\u4ef6\uff0c\u5b58\u50a8\u8def\u5f84\u4e3aLinux\u7cfb\u7edf\u4e0b\u7684\/tmp\u76ee\u5f55\uff0c\u800c\u6211\u4eec\u8fd8\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u573a\u666f\u9700\u8981\u5bf9\u4ea7\u751f\u7684\u4e34\u65f6\u6587\u4ef6\u7684\u540e\u7f00\u8fdb\u884c\u914d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u4e34\u65f6\u6587\u4ef6\u5728python\u9879\u76ee\u4e2d\u65f6\u5e38\u4f1a\u88ab\u4f7f\u7528\u5230\uff0c\u5176\u4f5c\u7528\u5728\u4e8e\u968f\u673a\u5316\u7684\u521b\u5efa\u4e0d\u91cd\u540d\u7684\u6587\u4ef6\uff0c\u8def\u5f84\u4e00\u822c\u90fd\u662f\u653e\u5728Linux\u7cfb\u7edf […]<\/p>\n","protected":false},"author":1482,"featured_media":206651,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-210722","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=210722"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210722\/revisions"}],"predecessor-version":[{"id":210724,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210722\/revisions\/210724"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/206651"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=210722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=210722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=210722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}