How to get the directory of the current script in Python

3 Answers

0 votes
import os

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


'''
run:

C:\Users\...\Python\test

'''

 



answered Mar 8, 2018 by avibootz
0 votes
import os 

dir_list = os.getcwd()

print(dir_list)


       
                  

'''
run:

/home

'''

 



answered Feb 22, 2023 by avibootz
0 votes
import os 

print(os.path.abspath(__file__))


       
                  

'''
run:

/home/main.py

'''

 



answered Feb 22, 2023 by avibootz

Related questions

1 answer 287 views
1 answer 204 views
1 answer 273 views
1 answer 129 views
2 answers 481 views
4 answers 214 views
214 views asked Jun 29, 2024 by avibootz
...