Python Call file path

Source: Internet
Author: User
Tags python script

1. Get the current execution main script method: Sys.argv[0] and _ file _

(1) SYS.ARGV

A list of instruction parameters to be passed to the Python script. Sys.argv[0] is the name of the script. Generally get a relative path, with Os.path.abspath (Sys.argv[0]) to get the absolute path of the execution file:

?
12 dirname, filename = os.path.split(os.path.abspath(sys.argv[0])) os.path.realpath(sys.argv[0])

If you execute sys.argv on the command line, return the interpreter path: ['/library/frameworks/python.framework/versions/3.6/bin/ipython3 ']

(2) _ _ File_ _

Obtain the path where the current execution module is located, typically a relative path, with Os.path.abspath (_ _ file_ _) to get the absolute path of the executable file:

?
12 dirname, filename = os.path.split(os.path.abspath( _ _ file_ _)) os.path.realpath(_ _ file_ _)

Note: Under the Python console, direct use of the print _ _ file _ is causing the name ' _ file _ ' is not defined error, because there is no script executed at this time, naturally no _ file _ _ Definition.

(3) Sys.argv[0] And _ _ File _ _ Difference: in the main execution file, there is no difference between the two, but if under different files, it is different, such as a.py,b.py two of the files

The execution a.py results are as follows:

As the result can be seen: if all are a.py the same result, but from the import of B in a sys.argv[0] or refers to the running main file: a.py and _ _ file_ _ but output is b.py

2.sys.path

A list of strings for the module search path. Initialized by the environment variable Pythonpath. is a list of directories, Sys.path[0] is the directory in which the current script that invokes the Python interpreter resides, the parent directory of the primary execution file.

Example: Execute in/user/ybp/a.py file a.py: print (sys.path[0]) ==>/USER/YBP,

Executes the return empty string at the command line;

3. Note that Os.path is a system environment variable, to differentiate from the above, there are no os.path[0],os.path methods:

(1) 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

(2) Os.path.realpath (path)

Returns the absolute path of a specific file name, which can be executed at the command line.

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

4.OS.GETCWD () returns the current working path, not necessarily in the script, this command is equivalent to PWD, can be executed on the command line, the return is an absolute path;

Python Call file path

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.