0 votes
1 view
in Python by (47.8k points)

Consider the following Python code:

import os

print os.getcwd()

I use os.getcwd() to get the script file's directory location. When I run the script from the command line it gives me the correct path whereas when I run it from a script run by code in a Django view it prints /.

How can I get the path to the script from within a script run by a Django view?

1 Answer

0 votes
by (107k points)

For finding script's directory with Python you need to call os.path.realpath on __file__, so that when __file__ is a filename without the path you still get the dir path below is the code example how we perform this:-

import os

print(os.path.dirname(os.path.realpath(__file__)))

Related questions

0 votes
1 answer
asked Jul 2, 2019 in Python by Sammy (47.8k points)
+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 16, 2019 in Python by Sammy (47.8k points)
+1 vote
2 answers
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...