python get file path method execution program (recommended)

Following small for everyone to share acquisition program execution method of a python file path (recommended), a good reference value, we want to help. Come and see, to follow the small series together
1. Get the current execution of the main script method: sys.argv [0] and _ file _

(1)sys.argv

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

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

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

(2)_ _ file_ _

Obtain the path of the current execution module resides, usually a relative path, get the absolute path of the executable file with os.path.abspath (_ _ file_ _):

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

Note: Python console directly print _ _ file _ will lead name '_ file _' is not defined wrong, because then not implemented in any script, there will be no _ file _ _ defines.

(3) sys.argv [0] and _ _ _ _ File differences: when the main executable file, no difference between the two, but if in a different file, is different, as a.py, b.py these two documents
execution a.py results are as follows:

As can be seen in FIG Results: If both results are a.py no different, but from the import sys.argv performed in a b [0] refers to a master file is run: a.py and _ _ file_ _ but output is b.py
2.sys.path

List of strings module search path. PYTHONPATH environment variable initialization get. Is a list of directories, sys.path [0] is the directory where the current script calls the Python interpreter, that is the main executable file of the parent directory.

Examples: As the execution file a.py /user/ybp/a.py: print (sys.path [0]) ==> / user / ybp,

In command line returns an empty string;

3. Note that the system environment variables os.path, to separate the upper region, not os.path [0], in some methods os.path:

(1)os.path.split(path)

The path name into head and tail couple. The tail will never be with a slash. If you enter a path that ends with a slash, then the resulting empty tail.

If the input path is not a slash, then the first portion is empty. If the input path is empty, then the resulting head and tail are empty.
https://docs.python.org/2/library/os.path.html#os.path.split
(2) the os.path.realpath (path)

Returns the absolute path to a specific file name, you can be executed from the command line.

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

4.os.getcwd () returns the current working directory, but does not necessarily need to be executed in the script, this command is equivalent to pwd, can be executed from the command line, it returns the absolute path;
I write to you, we recommend a very wide python learning resource gathering, click to enter , there is a senior programmer before learning to share experiences, study notes, there is a chance of business experience, and for everyone to carefully organize a python to combat zero-based item of information, day to you on the latest technology python, prospects, learning needs little comment on the details of
the above methods to perform this python get file path program (recommended) is small series to share the entire contents of all of the

Published 15 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/haoxun11/article/details/104908165