Gabriel Gabriel - 1 year ago 82
Python Question

sys.argv[0] is not showing the name of file in python

If I understand correctly, When typing sys.argv[0] it shows the name of the .py file one is working on. But when I do this, it shows me a different name:


The real name of the file is 'Ra x 13' and its located in 'C:\Users\infantjo\Documents\Python Scripts\Projects\Work in Progress'

The current directory is:

'C:\\Users\\infantjo\\Documents\\Python Scripts\\Projects'

What am I doing wrong?

(Im using Python 2.7.11 in Spyder 2.3.8 installed with Anaconda 2.3.0, 64bit).

Answer Source

argv contain a list of command line arguments passed to python script. argv[0] is the script name. doc

IDE like Spyder / PyCharm doesn't run the script direct. It's run the wrapper that run Your script.

In this case Spyder run the script that get a Your script name as a parameter and run it.

If You need a script name use a __file__ var.