0 votes
1 view
in Python by (6.5k 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 (107k 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 18, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
asked Jul 11, 2019 in Java by Ritik (3.5k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 18, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...