Home python why argv gives the full path to the script? (Python)

why argv [0] gives the full path to the script? (Python)

Author

Date

Category

Perform exercises from LearnPythonthehardway. Wherever ARGV is used, the author’s first argument contains the name of the script file (for example, “ex14.py”), I have the full path to this file – “D: _Mycode \ Python \ LPTHW \ EX14.PY” CMD). What does it depend on? What and where to change to argv [0] contained just the name of the script?


Answer 1, Authority 100%

What is it depends on?

Documentation for python & lt; script & gt; Forms start says that argv [0] contains what is specified on the command line:

If This Option is Given, The First Element Of SYS.ARGV Will Be
Script Name As Given On The Command Line.

If you see the full path, then the method of starting the script commands the full path python command (for example, when .py File extension is associated with python.exe on Windows).

That is, if you run Python your script.py on the command line (from the directory containing your script.py file), then sys.argv [0] will just your script.py '.

What and where you need to change to argv [0] contained just the name of the script?

Despite the fact that it is technically possible to override how the script is called, for example, create a BAT file that CD in Directory with a script before starting it (which can change its current working directory) , this should not be done, because you can get the required string in the script itself, not risking to break the launch of other python programs on the system.

If you want to get the name from argv [0] :

import os
Import sys.
PROGNAME = OS.PATH.BASENAME (SYS.ARGV [0])

In general, argv [0] is not required to specify a script on the disk, see Run a program with a changed process name .

The module can have __ File __ attribute from which you can also withdraw the file name. It is not always defined, for example, when the module is running using ExecFile () Built-in function. Related Question: How to Properly Determine Current Script Directory in Python? .

Also, the module name is not required to be associated with the file name, the module is not obliged at all in any file is located. If the module is executed as a script, then __ name__ == '__main __' .

In the subject of the command line used and its connection with Argv list: Full Command Line As It Was Typed .


Answer 2, Authority 150%

C: \ users \ xxx & gt; type t.py
  Import sys.
  Print SYS.Argv [0]
  Print __File__
  Import OS.
  Print OS.Path.Basename (SYS.ARGV [0])

Conclusion:

c: \ users \ xxx & gt; python ./t.py
./t.py.
./t.py.
T.py.

Answer 3, Authority 100%

depends on how you run the script.

path_to_python \ python.exe script.py

or

script.py

If the python folder and files with the * .py extension are added to the system paths associated with Python


Answer 4

Get the file name on the disk (regardless of the start place):

import os
Print (__ File__) # full path to the file
Print (OS.Path.BaseName (__ File__))

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions