{"id":4113,"date":"2024-01-23T17:41:48","date_gmt":"2024-01-23T09:41:48","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=4113"},"modified":"2024-01-23T18:03:50","modified_gmt":"2024-01-23T10:03:50","slug":"python%e8%8e%b7%e5%8f%96%e6%96%87%e4%bb%b6%e5%90%8d%e5%92%8c%e6%89%a9%e5%b1%95%e5%90%8d%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/xnw60rlg.ipyingshe.net:5347\/?p=4113","title":{"rendered":"python\u83b7\u53d6\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u7684\u65b9\u6cd5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1. \u91c7\u7528\u5207\u7247\u6cd5\u63d0\u53d6\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d<\/h2>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\uff0c\u6587\u4ef6\u540d\u3002\u5047\u8bbe\u5b83\u6709\u4e00\u4e2a\u4e09\u4e2a\u5b57\u6bcd\u7684\u6269\u5c55\u540d\uff0c\u5e76\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\uff0c\u627e\u5230\u6269\u5c55\u540d\u3002\u5bf9\u4e8e README.txt\uff0c\u6269\u5c55\u540d\u5e94\u8be5\u662f txt\u3002\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u7f16\u5199\u4ee3\u7801\uff0c\u5c06\u7ed9\u51fa\u6ca1\u6709\u6269\u5c55\u540d\u7684\u540d\u79f0\u3002\u60a8\u7684\u4ee3\u7801\u662f\u5426\u9002\u7528\u4e8e\u4efb\u610f\u957f\u5ea6\u7684\u6587\u4ef6\u540d\uff1f<\/p>\n\n\n\n<p>\u60a8\u7684\u4ee3\u7801\u662f\uff1a<\/p>\n\n\n\n<p>filename = &#8220;readme.txt&#8221;<br>extension = filename[-3:]<br>print(&#8220;The extension is&#8221;, extension)<br>name = filename[:len(filename)-4]<br>print(&#8220;The name without extension is&#8221;, name)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. os.path \u548c pathlib\u65b9\u6cd5\u63d0\u53d6\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d<\/h2>\n\n\n\n<p>\u5728 Python \u4e2d\uff0c\u6709\u4e0d\u540c\u7684\u65b9\u6cd5\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u5e76\u627e\u5230\u6587\u4ef6\u540d\u7684\u6269\u5c55\u540d\u3002<a href=\"https:\/\/bing.com\/search?q=translate+to+Chinese\">\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 os.path \u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a splitext() \u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u6587\u4ef6\u8def\u5f84\u5206\u5272\u4e3a\u6587\u4ef6\u540d\u548c\u6587\u4ef6\u6269\u5c55\u540d<\/a>\u3002<a href=\"https:\/\/translate.google.com\/\">\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 pathlib \u6a21\u5757\uff0c\u5b83\u6709\u4e00\u4e2a Path \u7c7b\uff0c\u5b83\u6709\u4e00\u4e2a suffix \u5c5e\u6027\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d<\/a>\u3002\u8fd9\u91cc\u6709\u4e00\u4e9b\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p>\u4f7f\u7528 os.path.splitext():import os<br>filename = &#8220;README.txt&#8221; # \u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\uff0c\u5305\u542b\u6587\u4ef6\u540d<br>name, extension = os.path.splitext(filename) # \u5206\u5272\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d<br>print(&#8220;The extension is&#8221;, extension) # \u6253\u5370\u6269\u5c55\u540d<br>print(&#8220;The name without extension is&#8221;, name) # \u6253\u5370\u6ca1\u6709\u6269\u5c55\u540d\u7684\u540d\u79f0<\/p>\n\n\n\n<p>\u4f7f\u7528 pathlib.Path().suffix:from pathlib import Path<br>filename = &#8220;README.txt&#8221; # \u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\uff0c\u5305\u542b\u6587\u4ef6\u540d<br>file_path = Path(filename) # \u521b\u5efa\u4e00\u4e2a Path \u5bf9\u8c61<br>extension = file_path.suffix # \u83b7\u53d6\u6269\u5c55\u540d<br>print(&#8220;The extension is&#8221;, extension) # \u6253\u5370\u6269\u5c55\u540d<br>name = file_path.stem # \u83b7\u53d6\u6ca1\u6709\u6269\u5c55\u540d\u7684\u540d\u79f0<br>print(&#8220;The name without extension is&#8221;, name) # \u6253\u5370\u6ca1\u6709\u6269\u5c55\u540d\u7684\u540d\u79f0<\/p>\n\n\n\n<p>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u9002\u7528\u4e8e\u4efb\u610f\u957f\u5ea6\u7684\u6587\u4ef6\u540d\uff0c\u53ea\u8981\u5b83\u4eec\u6709\u4e00\u4e2a\u70b9\uff08.\uff09\u5206\u9694\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u3002\u5982\u679c\u6587\u4ef6\u540d\u6ca1\u6709\u70b9\uff0c\u6216\u8005\u6709\u591a\u4e2a\u70b9\uff0c\u7ed3\u679c\u53ef\u80fd\u4f1a\u6839\u636e\u4f7f\u7528\u7684\u65b9\u6cd5\u800c\u4e0d\u540c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6587\u4ef6\u540d\u662f \u201c.bashrc\u201d\uff0cos.path.splitext() \u65b9\u6cd5\u4f1a\u8fd4\u56de (\u201c.bashrc\u201d, \u201c\u201d) \u4f5c\u4e3a\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\uff0c\u800c pathlib.Path().suffix \u65b9\u6cd5\u4f1a\u8fd4\u56de \u201c\u201d \u4f5c\u4e3a\u6269\u5c55\u540d\u3002\u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u6587\u4ef6\u540d\u662f \u201cfoo.bar.tar.gz\u201d\uff0cos.path.splitext() \u65b9\u6cd5\u4f1a\u8fd4\u56de (\u201cfoo.bar.tar\u201d, \u201c.gz\u201d) \u4f5c\u4e3a\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\uff0c\u800c pathlib.Path().suffix \u65b9\u6cd5\u4f1a\u8fd4\u56de \u201c.gz\u201d \u4f5c\u4e3a\u6269\u5c55\u540d\u3002\u5982\u679c\u4f60\u60f3\u83b7\u53d6\u4e00\u4e2a\u6709\u591a\u4e2a\u70b9\u7684\u6587\u4ef6\u540d\u4e2d\u7684\u6240\u6709\u6269\u5c55\u540d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 pathlib.Path().suffixes \u5c5e\u6027\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6269\u5c55\u540d\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0cpathlib.Path(\u201cfoo.bar.tar.gz\u201d).suffixes \u4f1a\u8fd4\u56de [\u201c.bar\u201d, \u201c.tar\u201d, \u201c.gz\u201d]\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Create a variable, filename. Assuming that it has a three letter extension, and using slice operations, find the extension. For README.txt, the extension should be txt. Write code using slice operations that will give the name without the extension. Does your code work on filenames of arbitrary length?<\/p>\n\n\n\n<p><code>filename = \"readme.txt\"<br>extension = filename[-3:]<br>print(extension)<br>name = filename[:len(filename)-4]<br>print(name)<\/code><\/p>\n\n\n\n<p>There are different ways to create a variable and find the extension of a filename in Python. <a href=\"https:\/\/stackoverflow.com\/questions\/541390\/extracting-extension-from-filename-in-python\/\">One way is to use the os.path module, which provides a function called splitext() that splits the file path into the file name and the file extension<\/a><a href=\"https:\/\/stackoverflow.com\/questions\/541390\/extracting-extension-from-filename-in-python\/\">1<\/a>. <a href=\"https:\/\/www.geeksforgeeks.org\/how-to-get-file-extension-in-python\/\">Another way is to use the pathlib module, which has a Path class that has a suffix property that can be used to get the file extension<\/a><a href=\"https:\/\/www.geeksforgeeks.org\/how-to-get-file-extension-in-python\/\">2<\/a>. Here are some examples of code using these methods:<\/p>\n\n\n\n<p>Using os.path.splitext():<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nfilename = \"README.txt\" # create a variable with the file name\nname, extension = os.path.splitext(filename) # split the file name and extension\nprint(\"The extension is\", extension) # print the extension\nprint(\"The name without extension is\", name) # print the name without extension<\/code><\/pre>\n\n\n\n<p>Using pathlib.Path().suffix:from pathlib <\/p>\n\n\n\n<p><code>import Path<br>filename = \"README.txt\" # create a variable with the file name<br>file_path = Path(filename) # create a Path object<br>extension = file_path.suffix # get the extension<br>print(\"The extension is\", extension) # print the extension<br>name = file_path.stem # get the name without extension<br>print(\"The name without extension is\", name) # print the name without extension<\/code><br>\u200b<\/p>\n\n\n\n<p>Both of these methods work on filenames of arbitrary length, as long as they have a dot (.) separating the name and the extension. If the filename does not have a dot, or has more than one dot, the results may vary depending on the method used. For example, if the filename is \u201c.bashrc\u201d, the os.path.splitext() method will return (\u201c.bashrc\u201d, \u201c\u201d) as the name and extension, while the pathlib.Path().suffix method will return \u201c\u201d as the extension. Similarly, if the filename is \u201cfoo.bar.tar.gz\u201d, the os.path.splitext() method will return (\u201cfoo.bar.tar\u201d, \u201c.gz\u201d) as the name and extension, while the pathlib.Path().suffix method will return \u201c.gz\u201d as the extension. <a href=\"https:\/\/www.geeksforgeeks.org\/how-to-get-file-extension-in-python\/\">If you want to get all the extensions in a filename with multiple dots, you can use the pathlib.Path().suffixes property, which returns a list of extensions<\/a><a href=\"https:\/\/www.geeksforgeeks.org\/how-to-get-file-extension-in-python\/\">2<\/a>. For example, pathlib.Path(\u201cfoo.bar.tar.gz\u201d).suffixes will return [\u201c.bar\u201d, \u201c.tar\u201d, \u201c.gz\u201d].<\/p>\n\n\n\n<p>\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>txt readme<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>The extension is .txt <br>The name without extension is README<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>The extension is .txt <br>The name without extension is README<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>1. \u91c7\u7528\u5207\u7247\u6cd5\u63d0\u53d6\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d \u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\uff0c\u6587\u4ef6\u540d\u3002\u5047\u8bbe\u5b83\u6709\u4e00\u4e2a\u4e09\u4e2a\u5b57\u6bcd\u7684 <span class=\"readmore\"><a href=\"http:\/\/xnw60rlg.ipyingshe.net:5347\/?p=4113\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-4113","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/4113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4113"}],"version-history":[{"count":4,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/4113\/revisions"}],"predecessor-version":[{"id":4119,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/4113\/revisions\/4119"}],"wp:attachment":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4113"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}