{"id":1072,"date":"2020-07-12T10:26:36","date_gmt":"2020-07-12T02:26:36","guid":{"rendered":"http:\/\/cnliutz.uicp.io\/?p=1072"},"modified":"2020-07-12T10:26:36","modified_gmt":"2020-07-12T02:26:36","slug":"python%e6%a3%80%e6%9f%a5%e6%96%87%e4%bb%b6%e6%88%96%e7%9b%ae%e5%bd%95%e6%98%af%e5%90%a6%e5%ad%98%e5%9c%a8","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=1072","title":{"rendered":"Python\u68c0\u67e5\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728"},"content":{"rendered":"\n<p><strong>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528Python\u68c0\u67e5\u6587\u4ef6\uff08\u6216\u76ee\u5f55\uff09\u662f\u5426\u5b58\u5728\u3002 \u4e3a\u4e86\u68c0\u67e5\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u4f7f\u7528python\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u75284\u79cd\u65b9\u6cd5\u6765\u9a8c\u8bc1\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u5b58\u5728\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/strong>os.path.exists()os.path.isfile()os.path.isdir()pathlibPath.exists()<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">os.path.exists\uff08\uff09<\/h1>\n\n\n\n<p><strong>\u4f7f\u7528path.exists()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5feb\u901f\u68c0\u67e5\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728\uff0c<\/strong><\/p>\n\n\n\n<p><strong>\u6b65\u9aa41\uff09\u5728\u8fd0\u884c\u4ee3\u7801\u4e4b\u524d\uff0c\u5bfc\u5165os.path\u6a21\u5757\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os.pathfrom os import path<\/code><\/pre>\n\n\n\n<p><strong>\u6b65\u9aa42\uff09\u4f7f\u7528path.exists\uff08\uff09\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>path.exists(\"guru99.txt\")<\/code><\/pre>\n\n\n\n<p><strong>\u6b65\u9aa43\uff09\u4ee5\u4e0b\u662f\u5b8c\u6574\u4ee3\u7801<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os.pathfrom os import pathdef main():   print (\"File exists:\"+str(path.exists('guru99.txt')))   print (\"File exists:\" + str(path.exists('career.guru99.txt')))   print (\"directory exists:\" + str(path.exists('myDirectory')))if __name__== \"__main__\":   main()<\/code><\/pre>\n\n\n\n<p><strong>\u65b9\u4fbf\u6d4b\u8bd5\uff0c\u6211\u4eec\u4ec5\u5728\u5de5\u4f5c\u76ee\u5f55\u4e2d\u521b\u5efa\u6587\u4ef6guru99.txt<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>File exists: TrueFile exists: Falsedirectory exists: False<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">os.path.isfile\uff08\uff09<\/h1>\n\n\n\n<p><strong>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528isfile()\u65b9\u6cd5\u6765\u68c0\u67e5\u7ed9\u5b9a\u7684\u8f93\u5165\u662f\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os.pathfrom os import pathdef main():\tprint (\"Is it File?\" + str(path.isfile('guru99.txt')))\tprint (\"Is it File?\" + str(path.isfile('myDirectory')))if __name__== \"__main__\":\tmain()<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Is it File? TrueIs it File? False<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">os.path.isdir\uff08\uff09<\/h1>\n\n\n\n<p><strong>\u5982\u679c\u8981\u786e\u8ba4\u7ed9\u5b9a\u8def\u5f84\u6307\u5411\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528os.path.dir\uff08\uff09\u51fd\u6570\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os.pathfrom os import pathdef main():   print (\"Is it Directory?\" + str(path.isdir('guru99.txt')))   print (\"Is it Directory?\" + str(path.isdir('myDirectory')))if __name__== \"__main__\":   main()<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Is it Directory? FalseIs it Directory? True<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u9002\u7528\u4e8ePython 3.4\u7684pathlibPath.exists\uff08\uff09<\/h1>\n\n\n\n<p><strong>Python 3.4\u53ca\u66f4\u9ad8\u7248\u672c\u5177\u6709pathlib\u6a21\u5757\uff0c\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u3002 \u5b83\u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pathlibfile = pathlib.Path(\"guru99.txt\")if file.exists ():    print (\"File exist\")else:    print (\"File not exist\")<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>File exist<\/code><\/pre>\n\n\n\n<p><strong>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import osfrom os import pathdef main():      print(os.name)print(\"Item exists:\" + str(path.exists(\"guru99.txt\")))print(\"Item is a file: \" + str(path.isfile(\"guru99.txt\")))print(\"Item is a directory: \" + str(path.isdir(\"guru99.txt\")))if __name__ == \"__main__\":    main()<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Item exists: TrueItem is a file: TrueItem is a directory: False<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>\u603b\u7ed3\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u5982\u4f55\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ol class=\"wp-block-list\"><li>os.path.exists\uff08\uff09\u2013\u5982\u679c\u8def\u5f84\u6216\u76ee\u5f55\u5b58\u5728\uff0c\u5219\u8fd4\u56deTrue\u3002<\/li><li>os.path.isfile\uff08\uff09\u2013\u5982\u679c\u8def\u5f84\u4e3aFile\uff0c\u5219\u8fd4\u56deTrue\u3002<\/li><li>os.path.isdir\uff08\uff09-\u5982\u679c\u8def\u5f84\u662fDirectory\uff0c\u5219\u8fd4\u56deTrue\u3002<\/li><li>pathlib.Path.exists\uff08\uff09-\u5982\u679c\u8def\u5f84\u6216\u76ee\u5f55\u5b58\u5728\uff0c\u5219\u8fd4\u56deTrue\u3002 \uff08\u8981\u6c42\u5728Python 3.4\u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\u5e94\u7528\uff09<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528Python\u68c0\u67e5\u6587\u4ef6\uff08\u6216\u76ee\u5f55\uff09\u662f\u5426\u5b58\u5728\u3002 \u4e3a\u4e86\u68c0\u67e5 <span class=\"readmore\"><a href=\"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=1072\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,10],"tags":[],"class_list":["post-1072","post","type-post","status-publish","format-standard","hentry","category-2","category-python"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1072"}],"version-history":[{"count":1,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1072\/revisions"}],"predecessor-version":[{"id":1073,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1072\/revisions\/1073"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1072"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}