'Using __file__ in script to get current file directory of script and executable file
I'm trying to write universal scripts that would work on any PC that meets requirements and I have a little problem with getting directories and paths right. Currently I have two folders in one location: C:\Users\myProject, one with my Scripts and other named Tests. In Scripts folder I store my python scripts with all instructions and in Tests I have .bat files used to execute scripts. To get parents path I decided to use (__file__), for example in my script - Test_Script.py:
__file__ = 'Test_Script.py'
print(r'__file__', __file__)
curr_path = os.path.dirname(os.path.abspath(__file__))
print (curr_path)
And it works, but in two ways. If I run cmd in folder with Scripts and type: py Test_Script.py, I get:
Test_Script.py
C:\Users\myProject\Scripts
But when I run it from Tests directory, I get:
Test_Script.py
C:\Windows\system32
How do I fix this to have same output as in first case while running script via cmd?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
