Back

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

I'm building a simple helper script for work that will copy a couple of template files in our codebase to the current directory. I don't, however, have the absolute path to the directory where the templates are stored. I do have a relative path from the script but when I call the script it treats that as a path relative to the current working directory. Is there a way to specify that this relative URL is from the location of the script instead?

1 Answer

0 votes
by (106k points)

To get the relative paths in Python you want to do something like as follows:-

import os 

dirname = os.path.dirname(__file__) 

filename = os.path.join(dirname, 'relative/path/to/file/you/want')

Related questions

0 votes
1 answer
asked Jul 26, 2019 in Python by selena (1.6k points)
0 votes
1 answer
asked Nov 24, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Nov 12, 2019 in Java by Anvi (10.2k points)

Browse Categories

...