Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (7.3k points)

How do I get the path of a Python script I am running in? I was doing dirname(sys.argv[0]), however, on Mac, I only get the filename - not the full path as I do on Windows.

No matter where my application is launched from, I want to open files that are relative to my script file(s).

1 Answer

0 votes
by (106k points)

There are many ways to get the path of a Python script that you are running some of the important ways are as follows:-

The first thing you can use is os.path.realpath(__file__), which will give you the path of the current file.

Another way you have to use so.path .abspath() function below is the code for the same:-

import os

print os.path.abspath(__file__)

Related questions

0 votes
1 answer
asked Jul 11, 2019 in Java by Ritik (3.5k points)
0 votes
2 answers
asked Jul 18, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 18, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...