How Python Gets the directory that is currently located

Source: Internet
Author: User
This article is to explain the use of Python to get the current directory of the method and related examples, very clear and simple, the need for small partners can refer to the following

Sys.path

A list of strings for the module search path. Initialized by the environment variable Pythonpath.

SYS.PATH[0] is the directory that contains the current script that invokes the Python interpreter.

sys.argv

A list of instruction parameters to be passed to the Python script.

Sys.argv[0] is the name of the script (the system determines whether it is the full name)

Assuming that the Python command is displayed, such as Python demo.py, the absolute path is obtained;

If you execute scripts directly, such as./demo.py, you get a relative path.

os.getcwd()

Gets the current working path. Here is the absolute path.
Https://docs.python.org/2/library/os.html#os.getcwd

__file__

Obtain the path where the module is located, and the relative path may be obtained.

If the display executes Python, an absolute path is obtained.

If you execute the script directly by a relative path ./pyws/path_demo.py , you get a relative path.

To get an absolute path, call theos.path.abspath()

Some methods in the Os.path

Os.path.split (PATH)

Divides the path name into a pair of headers and tails. The tail will never have a slash. If the input path ends with a slash, then the empty tail is obtained.

If the input path does not have a slash, then the head part is empty. If the input path is empty, then the resulting header and tail are empty.
Https://docs.python.org/2/library/os.path.html#os.path.split

Os.path.realpath (PATH)

Returns the absolute path of a specific file name.

Https://docs.python.org/2/library/os.path.html#os.path.realpath

code example

Environment Win7, Python2.7

Taking/e/pyws/path_demo.py as an example


#!/usr/bin/env pythonimport osimport sysif __name__ = = ' __main__ ':  print "sys.path[0] =", sys.path[0]  print "sys . argv[0] = ", sys.argv[0]  print" __file__ = ", __file__  print" Os.path.abspath (__file__) = ", Os.path.abspath (__ file__)  print "Os.path.realpath (__file__) =", Os.path.realpath (__file__)  print "Os.path.dirname ( Os.path.realpath (__file__)) = ", Os.path.dirname (Os.path.realpath (__file__))  print" Os.path.split ( Os.path.realpath (__file__)) = ", Os.path.split (Os.path.realpath (__file__))  print" os.getcwd () = ", OS.GETCWD ()

Run in/d, output is


$ python/e/pyws/path_demo.pysys.path[0] = e:\pywssys.argv[0] = e:/pyws/path_demo.py__file__ = E:/pyws/path_ Demo.pyos.path.abspath (__file__) = E:\pyws\path_demo.pyos.path.realpath (__file__) = E:\pyws\path_ Demo.pyos.path.dirname (Os.path.realpath (__file__)) = E:\pywsos.path.split (Os.path.realpath (__file__)) = (' e:\\ Pyws ', ' path_demo.py ') os.getcwd () = D:\

Execute scripts directly in the E-drive with the command line


$./pyws/path_demo.pysys.path[0] = e:\pywssys.argv[0] =./pyws/path_demo.py__file__ =./pyws/path_ Demo.pyos.path.abspath (__file__) = E:\pyws\path_demo.pyos.path.realpath (__file__) = E:\pyws\path_ Demo.pyos.path.dirname (Os.path.realpath (__file__)) = E:\pywsos.path.split (Os.path.realpath (__file__)) = (' e:\\ Pyws ', ' path_demo.py ') os.getcwd () = E:\

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.