Hướng dẫn get last directory in path python - lấy thư mục cuối cùng trong python đường dẫn
Ngày đăng:
21/10/2022
Trả lời:
0
Lượt xem:
190
Trong các dự án hiện tại của tôi, tôi thường chuyển các phần phía sau của một đường dẫn đến một hàm và do đó sử dụng mô -đun ____1010.Để có được phần N-th theo thứ tự ngược lại, tôi đang sử dụng:
Hơn nữa, để vượt qua phần n-th theo thứ tự ngược của đường dẫn chứa đường dẫn còn lại, tôi sử dụng:
Lưu ý rằng hàm này trả về 0Object có thể dễ dàng chuyển đổi thành chuỗi (ví dụ: 2)
{"id":978,"title":"Python How To Get The Last Directory Name In A Path","advertisement_flag":false,"content":"\n\u003cp\u003eThis short post shows you various ways to get the last directory's name in a path by Python.\u003c/p\u003e\n\n\u003cp\u003eSuppose this is the path you want to extract: \u003cbr\u003e\u003c/p\u003e\n\n\u003cpre\u003e\n\npath =\"/test1/test2/test3/test.txt\"\n\n\u003c/pre\u003e\n\n\u003ch2\u003eMethod 1: os.path.basename\u003c/h2\u003e\n\n\u003cp\u003eos.path.basename: Returns the final component of a pathname \u003cbr\u003e\u003c/p\u003e\n\n\u003cpre\u003e\n\nprint os.path.basename(path) # 'test.txt'\n\nprint os.path.dirname(path) # '/test1/test2/test3'\n\nprint os.path.basename(os.path.dirname(path)) # 'test3'\n\n\u003c/pre\u003e\n\n\u003ch2\u003eMethod 2: os.path.split\u003c/h2\u003e\n\n\u003cp\u003eos.path.split: Return tuple (head, tail) where tail is everything after the final slash.\u003c/p\u003e\n\n\u003cpre\u003e\n\nprint os.path.split(path) #('/test1/test2/test3', 'test.txt')\n\nprint os.path.dirname(path) # '/test1/test2/test3'\n\nprint os.path.split(os.path.dirname(path)) # ('/test1/test2', 'test3')\n\nprint os.path.split(os.path.dirname(path))[1] # 'test3'\n\n\u003c/pre\u003e\n","created_at":"2020-11-13T00:57:05.833Z","author_name":"Bob","tag_groups":["python|Python|false"],"author_id":1,"slug":"python-how-to-get-the-last-directory-name-in-a-path","parent_id":null,"visits":16015,"likes":0,"image":null} [] [] python [] [{"id":973,"name":"MYOB Account Right Enterprise Setup","short_content":"This post shows you how to set up MYOB Account Right Enterprise in a terminal environment.\n\nFirstly, Microsoft Office such as Word and Excel, and Micro ...","likes":3,"slug":"myob-account-right-enterprise-setup","tag_groups":["business|Business|false","myob|MYOB|false"],"image":null},{"id":976,"name":"Python Alternative For Switch Statements","short_content":"This snippet shows you one of the approaches to implement switch statements in python.\n\n\n\ndef Method1(argument):\n\n pass\n\n\n\ndef Method2(argument):\n\n ...","likes":3,"slug":"python-alternative-for-switch-statements","tag_groups":["python|Python|false"],"image":null},{"id":977,"name":"How To Use Apache Ant With Eclipse","short_content":"This post helps you set up Eclipse to use Apache Ant.\r\n\r\nThe steps include: \r\n\r\n Set up Apache Ant\r\n Configure Eclipse\r\n How to use Ant Edi ...","likes":4,"slug":"how-to-use-apache-ant-with-eclipse","tag_groups":[],"image":null},{"id":975,"name":"C# How To Convert DataTable To List Of Entities","short_content":"This snippet helps you convert a datatable to a list of entities.\n\n\n\nusing System.Data;\n\nusing System.Reflection;\n\n\n\npublic static List convert(DataTab ...","likes":1,"slug":"c-how-to-convert-datatable-to-list-of-entities","tag_groups":[],"image":null},{"id":974,"name":"C# How To Pass A Property As A Variable","short_content":"This post shows you how to pass a property as a variable in C#.\n\nLet's suppose a scenario from which we can understand what we want to achieve and how ...","likes":0,"slug":"c-how-to-pass-a-property-as-a-variable","tag_groups":[],"image":null}] main-forum [{"id":68,"slug":"linux","name":"Linux","post_name":"Linux"},{"id":69,"slug":"django","name":"Django","post_name":"Django"},{"id":70,"slug":"python","name":"Python","post_name":"Python"},{"id":71,"slug":"sugarcrm","name":"SugarCRM","post_name":"SugarCRM"},{"id":73,"slug":"agile","name":"Agile","post_name":"Agile"},{"id":75,"slug":"business","name":"Business","post_name":"Business"},{"id":76,"slug":"myob","name":"MYOB","post_name":"MYOB"},{"id":77,"slug":"edi","name":"EDI","post_name":"EDI"},{"id":78,"slug":"database","name":"Database","post_name":"Database"},{"id":72,"slug":"project_management","name":"Project Management","post_name":"Project Management"},{"id":74,"slug":"microsoft_office","name":"Microsoft Office","post_name":"Microsoft Office"}] |