Home  >  Article  >  Backend Development  >  python method to get program execution file path

python method to get program execution file path

不言
不言Original
2018-04-27 10:16:016918browse

The following is a Python method to obtain the program execution file path (recommended). It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together

1. Get the currently executed main script method: sys.argv[0] and _ file _

(1)sys.argv

A list of command parameters passed to the Python script. sys.argv[0] is the name of the script. Generally, the relative path is obtained. Use os.path.abspath(sys.argv[0]) to get the absolute path of the executable file:

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

If in the command Executing sys.argv returns the interpreter path: ['/Library/Frameworks/Python.framework/Versions/3.6/bin/ipython3']

(2)_ _ file_ _

Get the path of the current execution module, which is generally a relative path. Use os.path.abspath(_ _ file_ _) to get the absolute path of the execution file:

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

Note: Under the Python console, using print _ _ file _ directly will cause the name ' _ file _ ' is not defined error, because it is not under any script at this time. Execution, naturally there is no definition of _ file _ _.

(3) The difference between sys.argv[0] and _ _ file _ _: There is no difference between the two when they are in the main execution file, but if It will be different under different files, such as a.py and b.py.

The results of executing a.py are as follows:

As can be seen from the results above: if they are both a.py, the results are the same, but from the The sys.argv[0] executed when b is imported in still refers to the main running file: a.py, but _ _ file_ _ outputs b.py

2. sys.path

# A string list of module search paths. Initialized by the environment variable PYTHONPATH. is a list of directories, sys.path[0] is the directory where the current script that calls the Python interpreter is located, that is, the parent directory of the main execution file.

Example: If executed in the file a.py of /user/ybp/a.py: print(sys.path[0])==>/user/ybp,

Executing on the command line returns an empty string;

3. Note that os.path is a system environment variable, which should be distinguished from the above. There is no os.path[0], os.path Some methods in:

(1)os.path.split(path)

Split the path name into A pair of head and tail. There is never a trailing slash. If the input path ends with a slash, an empty trailer is obtained.

If the input path does not have a slash, the header field will be empty. If the input path is empty, the resulting head 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 on the command line.

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

4.os.getcwd() What is returned is the current working path, and does not necessarily need to be executed in a script. This command is equivalent to pwd and can be executed in the command line. The absolute path is returned;

Related recommendations :

Python method to obtain specified data from csv text


##

The above is the detailed content of python method to get program execution file path. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn