Environment Python 3.9.6; Windows 10
Q1. Why do I get different results when using 'sys.argv'?
Full path of the script file >> "C:\example\test.py"
Code
import sys
print(sys.argv[0])
Then below are the results.
If I execute it via CMD like "python test.py",
test.py
If I execute it via IDLE or VS CODE,
C:\example\test.py
Q2. What is the best way to get the directory of a 'main' script file?
I need "SOMETHING_HERE" below that gives "C:\example" as a result.
if __name__ == "__main__":
result = SOMETHING_HERE()
print(result)
"sys.argv" is not an option regarding the problem in Q1. Neither is "os.getcwd()" because it literally gives 'current working folder' which is not the exact result I need. Is there any other option that I can get the directory safely?