1

I am using argparse in a python file "hello.py"

I am executing this file using

python hello.py -infile "ex1.txt" -op1 "2"  ....

How to get which directory "hello.py" file is located? I know that os.path.dirname() can be used for infile, but how to know the same thing "hello.py"?

learner
  • 2,582
  • 9
  • 43
  • 54

1 Answers1

0

try __file__ for full file name :-)

ngulam
  • 995
  • 1
  • 6
  • 11
  • `__file__` is usually **not** the full pathname, though. – Martijn Pieters Jun 10 '14 at 15:18
  • In which cases it isn't? I tried before posting, putting `print __file__` in `main` body (Python 2.7.6) – ngulam Jun 10 '14 at 15:28
  • 1
    In scripts started from the Python command line. `__file__` is equal to `sys.argv[0]` in that case; the same path used on the command line. If you started the script with a relative pathname, so is `__file__` a relative pathname. The correct way to use `__file__` is to make it absolute first (with `os.path.absname(__file__)`). – Martijn Pieters Jun 10 '14 at 15:30
  • import os print(os.getcwd()) – amandeep1991 Apr 01 '18 at 16:56