Two ways to get the current absolute path of a file in Python3

Source: Internet
Author: User
Below for you to share a python3 in the current absolute path of the two methods, with a good reference value, I hope to be helpful to everyone. Come and see it together.

Import Sysprint (SYS.ARGV)

Get a list of the file's current absolute path strings

[' d:/pycharm/practiceproject/clientservernetworking.py ']

Method 2:

Import Osprint (OS.GETCWD ()) print (Os.listdir ()) Print (Os.path.join (OS.GETCWD (), Os.listdir () [1]) D:\pycharm\ practiceproject['. Idea ', ' clientservernetworking.py ']d:\pycharm\practiceproject\clientservernetworking.py

os.getcwd () Gets the current working path, which does not include the current file name clientservernetworking.py,

Os.listdir () gets a list of the names of files and folders under the current path

The final step stitching the path to get the absolute path of the current file string

Note: Method 1 and method 21 slash slash a backslash

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.