Python obtains the current path implementation code, and python obtains the current path

Source: Internet
Author: User

Python obtains the current path implementation code, and python obtains the current path

Python obtains the current path implementation code

Import OS, sys

Use sys. path [0], sys. argv [0], OS. getcwd (), OS. path. abspath (_ file _), OS. path. realpath (_ file __)

Sys. path is the search path list of the module that Python will search for. sys. path [0] and sys. argv [0] is one thing, because Python will automatically set sys. add argv [0]

Sys. path.

If you execute python getpath \ getpath in the C: \ test directory. py, then OS. getcwd () will output "C: \ test", sys. path [0] outputs "C: \ test \ getpath ".

If you use the py2exe module to compile the Python script as an executable file, the output of sys. path [0] will change:

If the dependent library is packaged as a zip file by default, sys. path [0] will output "C: \ test \ getpath \ libarary.zip ";

If. if the zipfile = None parameter is specified in py, the dependent library will be packaged into the exe file, so sys. path [0] outputs "C: \ test \ getpath \ getpath.exe ".

#!/bin/env python#-*- encoding=utf8 -*-import os,sysif __name__=="__main__":  print "__file__=%s" % __file__  print "os.path.realpath(__file__)=%s" % os.path.realpath(__file__)  print "os.path.dirname(os.path.realpath(__file__))=%s" % os.path.dirname(os.path.realpath(__file__))    print "os.path.split(os.path.realpath(__file__))=%s" % os.path.split(os.path.realpath(__file__))[0]    print "os.path.abspath(__file__)=%s" % os.path.abspath(__file__)  print "os.getcwd()=%s" % os.getcwd()  print "sys.path[0]=%s" % sys.path[0]  print "sys.argv[0]=%s" % sys.argv[0]

Output result:

D:\>python ./python_test/test_path.py__file__=./python_test/test_path.pyos.path.realpath(__file__)=D:\python_test\test_path.pyos.path.dirname(os.path.realpath(__file__))=D:\python_testos.path.split(os.path.realpath(__file__))=D:\python_testos.path.abspath(__file__)=D:\python_test\test_path.pyos.getcwd()=D:\sys.path[0]=D:\python_testsys.argv[0]=./python_test/test_path.py

OS. getcwd () "D: \", which is the starting execution directory.

Sys. path [0] Or sys. argv [0] "D: \ python_test", where the directory where the script is initially executed

OS. path. split (OS. path. realpath (_ file _) [0] "D: \ python_test", where the directory of the test_path.py file where _ file _ is located

Obtain the current path correctly:

_ File _ is the currently executed file # obtain the path of the current file _ print "OS. path. realpath (_ file _) = % s "% OS. path. realpath (_ file _) # obtain the directory of the current file _ print "OS. path. dirname (OS. path. realpath (_ file _) = % s "% OS. path. dirname (OS. path. realpath (_ file _) # obtain the directory where the current file _ is located print "OS. path. split (OS. path. realpath (_ file _) = % s "% OS. path. split (OS. path. realpath (_ file _) [0]

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

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.