{"id":1733,"date":"2014-12-23T17:14:02","date_gmt":"2014-12-23T17:14:02","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1733"},"modified":"2014-12-23T17:14:02","modified_gmt":"2014-12-23T17:14:02","slug":"python%e7%9a%84%e4%b8%80%e4%ba%9b%e5%b0%8f%e7%9f%a5%e8%af%86%e7%82%b9_4","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1733.html","title":{"rendered":"Python\u7684\u4e00\u4e9b\u5c0f\u77e5\u8bc6\u70b9_4"},"content":{"rendered":"<h5>Python\u6253\u5f00\u6587\u4ef6\u7684\u6700\u4f73\u5b9e\u8df5<\/h5>\n<p>\u5e26\u6709try\u7684open()\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"lang:default decode:true\">file = open(\"\/tmp\/foo.txt\")\ntry:\n    data = file.read()\nfinally:\n    file.close()<\/pre>\n<p>\u7b80\u6d01\u660e\u5feb\u7684with\u8bed\u53e5\uff1a<\/p>\n<pre class=\"lang:default decode:true\">with open(\"\/tmp\/foo.txt\") as file:\n    data = file.read()<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/9282967\/how-to-open-a-file-using-the-open-with-statement\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/9282967\/how-to-open-a-file-using-the-open-with-statement<\/a><\/li>\n<li><a href=\"http:\/\/python.42qu.com\/11155501\" target=\"_blank\">\u7406\u89e3Python\u7684With\u8bed\u53e5- linbo<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/largetalk\/article\/details\/6910277\" target=\"_blank\">python with\u7528\u6cd5<\/a><\/li>\n<\/ul>\n<hr \/>\n<h5>\u5982\u4f55\u4f7f\u7528Python\u5224\u65ad\u4e00\u4e2a\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1f<\/h5>\n<pre class=\"lang:default decode:true\">You can use:\n\nimport os\nos.path.isfile(fname)\n\nif you need to be sure it's a file.\n====\nYou have the os.path.exists function:\n\nimport os.path\nos.path.exists(file_path)\n\nThis returns True for both files and directories.\nUse os.path.isfile to test if it's a file specifically.\n====\nUnlike isfile(), exists() will yield True for directories.\nSo depending on if you want only plain files or also directories, you'll use isfile() or exists().\n\n&gt;&gt;&gt; print os.path.isfile(\"\/etc\/passwd\")\nTrue\n&gt;&gt;&gt; print os.path.isfile(\"\/etc\")\nFalse\n&gt;&gt;&gt; print os.path.isfile(\"\/does\/not\/exist\")\nFalse\n&gt;&gt;&gt; print os.path.exists(\"\/etc\/passwd\")\nTrue\n&gt;&gt;&gt; print os.path.exists(\"\/etc\")\nTrue\n&gt;&gt;&gt; print os.path.exists(\"\/does\/not\/exist\")\nFalse<\/pre>\n<p>\u5373\uff1a<br \/>\nimport os<br \/>\nos.path.isfile(fname)<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/82831\/check-if-a-file-exists-using-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/82831\/check-if-a-file-exists-using-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/8933237\/how-to-find-if-directory-exists-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/8933237\/how-to-find-if-directory-exists-in-python<\/a><\/li>\n<\/ul>\n<hr \/>\n<h5>\u5728Linux\u4e0b\u67e5\u770b\u6240\u6709\u6253\u5f00\u7684\u6587\u4ef6\u2014\u2014\u7528Python\u5b9e\u73b0<\/h5>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?q=linux+use+python+implement+lsof\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=linux+use+python+implement+lsof<\/a><\/li>\n<li><a href=\"https:\/\/pypi.python.org\/pypi\/psutil\" target=\"_blank\">https:\/\/pypi.python.org\/pypi\/psutil<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/20106220\/check-for-open-files-with-python-in-linux\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/20106220\/check-for-open-files-with-python-in-linux<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/11114492\/check-if-a-file-is-not-open-not-used-by-other-process-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/11114492\/check-if-a-file-is-not-open-not-used-by-other-process-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/2023608\/check-what-files-are-open-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/2023608\/check-what-files-are-open-in-python<\/a><\/li>\n<\/ul>\n<hr \/>\n<h5>Python\u7684split()\u65b9\u6cd5<\/h5>\n<pre class=\"lang:default decode:true\">str.split([sep[, maxsplit]])\n\nReturn a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).\nIf sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example,'1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1&lt;&gt;2&lt;&gt;3'.split('&lt;&gt;') returns ['1','2', '3']). Splitting an empty string with a specified separator returns [''].\nIf sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].\nFor example, ' 1 2 3 '.split() returns ['1', '2', '3'], and ' 1 2 3 '.split(None, 1) returns ['1', '2 3 '].<\/pre>\n<p>\u6d4b\u8bd5\u7528\u4f8b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">ttt = '''\nGET http:\/\/justin.ixyzero.com\/app\/styles\/justin.css HTTP\/1.1\nAccept: text\/css, *\/*\nContent-Type: text\/css\n=====12345=====\nContent-Type: text\/css\nContent-Type: text\/css\n=====12345=====\nContent-Type: text\/css\nContent-Type: text\/css\nContent-Type: text\/css'''\n\nfind_str = ttt.split('=====12345=====')\nfind_str = ttt.split('=====12345=====', 1)<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<p><a href=\"https:\/\/docs.python.org\/2\/library\/stdtypes.html#str.split\" target=\"_blank\">https:\/\/docs.python.org\/2\/library\/stdtypes.html#str.split<\/a><\/p>\n<hr \/>\n<h5>Python\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/h5>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"https:\/\/docs.python.org\/2\/howto\/regex.html\" target=\"_blank\">https:\/\/docs.python.org\/2\/howto\/regex.html<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/re.html\" target=\"_blank\">https:\/\/docs.python.org\/2\/library\/re.html<\/a><\/li>\n<li><a href=\"http:\/\/www.cnblogs.com\/huxi\/archive\/2010\/07\/04\/1771073.html\" target=\"_blank\">Python\u6b63\u5219\u8868\u8fbe\u5f0f\u6307\u5357<\/a><br \/>\n<a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/opensource\/os-cn-pythonre\/\" target=\"_blank\">\u4f7f\u7528 Python \u7684re\u6a21\u5757<\/a><br \/>\n<a href=\"http:\/\/deerchao.net\/tutorials\/regex\/regex.htm#backreference\" target=\"_blank\">\u6b63\u5219\u8868\u8fbe\u5f0f30\u5206\u949f\u5165\u95e8\u6559\u7a0b<\/a>\u00a0#\u975e\u6355\u83b7<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">(?:...)\nA non-capturing version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group cannot be retrieved after performing a match or referenced later in the pattern.<\/pre>\n<h6>\u8fd8\u6709\u5c31\u662f\u6ce8\u610f\uff1a<\/h6>\n<p>search() \/ match() \u7684\u4e0d\u540c\uff1amatch() \u51fd\u6570\u53ea\u5728\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u4f4d\u7f6e\u5c1d\u8bd5\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4e5f\u5c31\u662f\u53ea\u62a5\u544a\u4ece\u4f4d\u7f6e 0 \u5f00\u59cb\u7684\u5339\u914d\u60c5\u51b5\uff0c\u800c search() \u51fd\u6570\u662f\u626b\u63cf\u6574\u4e2a\u5b57\u7b26\u4e32\u6765\u67e5\u627e\u5339\u914d\u3002\u5982\u679c\u60f3\u8981\u641c\u7d22\u6574\u4e2a\u5b57\u7b26\u4e32\u6765\u5bfb\u627e\u5339\u914d\uff0c\u5e94\u5f53\u7528 search()\uff1b<\/p>\n<p>findall() \/ finditer() \u7684\u4e0d\u540c\u3002<\/p>\n<hr \/>\n<h5>\u5728Python\u4e2d\u68c0\u6d4blist\u662f\u5426\u4e3a\u7a7a\u7684\u6700\u4f73\u65b9\u5f0f<\/h5>\n<pre class=\"lang:default decode:true \">#Using the implicit booleanness of the empty list is quite pythonic.\nif not a:\n\tprint \"List is empty\"\n\n####\nThe pythonic way to do it is from the style guide[ http:\/\/www.python.org\/dev\/peps\/pep-0008\/ ]:\n\nFor sequences, (strings, lists, tuples), use the fact that empty sequences are false.\n\nYes:\nif seq:\nif not seq:\n\nNo:\nif len(seq):\nif not len(seq):\n####\n\nif len(seq) == 0:\n\tprint 'the list is empty'\n\n####\n\nAn empty list is itself considered false in true value testing (see https:\/\/docs.python.org\/2\/library\/stdtypes.html#truth-value-testing):\n\na = []\nif a:\n\tprint \"not empty\"<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/53513\/best-way-to-check-if-a-list-is-empty\" target=\"_blank\">python &#8211; Best way to check if a list is empty<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/stdtypes.html#truth-value-testing\" target=\"_blank\">https:\/\/docs.python.org\/2\/library\/stdtypes.html#truth-value-testing<\/a><\/li>\n<li><a href=\"http:\/\/www.python.org\/dev\/peps\/pep-0008\/\" target=\"_blank\">http:\/\/www.python.org\/dev\/peps\/pep-0008\/<\/a><\/li>\n<\/ul>\n<hr \/>\n<h5><strong>\u5728<\/strong><strong>Python<\/strong><strong>\u4e2d\u5408\u5e76<\/strong><strong>list<\/strong><strong>\u7684\u6700\u4f73\u65b9\u5f0f<\/strong><\/h5>\n<p>\u641c\u7d22\u5173\u952e\u5b57\uff1a<a href=\"http:\/\/cn.bing.com\/search?q=python+merge+two+list+efficient\" target=\"_blank\">http:\/\/cn.bing.com\/search?q=python+merge+two+list+efficient<\/a><\/p>\n<pre class=\"lang:default decode:true \">What is the most efficient way to concatenate two lists list_a and list_b when:\n    list_b items have to be placed before list_a items\n    the result must be placed in list_a\n\nI have 4 possibilities in mind:\n\n# 1\nlist_a = list_b + list_a\n\n# 2\nfor item in list_b:\n    list_a.insert(0, item)\n\n# 3\nfor item in self.list_a:\n    list_b.append(item)\nlist_a = list_b\n\n# 4\nlist_a[0:0] = list_b\n\nThanks!<\/pre>\n<p>\u5373\uff0c\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u63a8\u8350\u4f7f\u7528\u7b2c\u4e00\u79cd\u65b9\u5f0f\uff1a<\/p>\n<p>list_a = list_b + list_a<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/12088089\/python-list-concatenation-efficiency\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/12088089\/python-list-concatenation-efficiency<\/a>\u00a0\u00a0 #Python\u4e2d\u5404\u79cdlist\u5408\u5e76\u65b9\u6cd5\u7684\u6548\u7387\u5bf9\u6bd4<\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/11574195\/how-to-merge-multiple-lists-into-one-list-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/11574195\/how-to-merge-multiple-lists-into-one-list-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/1720421\/merge-two-lists-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/1720421\/merge-two-lists-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/22642261\/python-concatenate-2-lists\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/22642261\/python-concatenate-2-lists<\/a><\/li>\n<\/ul>\n<h6>\u53e6\uff0c\u5c06\u5d4c\u5957\u5217\u8868\u8f6c\u6362\u6210\u4e00\u7ef4\u5217\u8868\u7684\u65b9\u6cd5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/8327856\/how-to-extract-nested-lists\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/8327856\/how-to-extract-nested-lists<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/3046217\/join-a-list-of-lists-together-into-one-list-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/3046217\/join-a-list-of-lists-together-into-one-list-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/2158395\/flatten-an-irregular-list-of-lists-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/2158395\/flatten-an-irregular-list-of-lists-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/10823877\/what-is-the-fastest-way-to-flatten-arbitrarily-nested-lists-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/10823877\/what-is-the-fastest-way-to-flatten-arbitrarily-nested-lists-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/406121\/flattening-a-shallow-list-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/406121\/flattening-a-shallow-list-in-python<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python\u6253\u5f00\u6587\u4ef6\u7684\u6700\u4f73\u5b9e\u8df5 \u5e26\u6709try\u7684open()\u65b9\u6cd5\uff1a file = open(&#8220;\/tmp\/foo.t [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[8,19],"class_list":["post-1733","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-python","tag-tips"],"views":2729,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1733"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1733\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}