You can create a path relative to a module by using a module’s __file__
attribute. For example:
myfile = open(os.path.join(
os.path.dirname(__file__),
MY_FILE))
This should do what you want regardless of where you start your script.
More Related Contents:
- Adding Python to PATH on Windows
- Relative paths in Python
- How to get an absolute file path in Python
- Where is Python’s sys.path initialized from?
- How to refer to relative paths of resources when working with a code repository
- Open file in a relative location in Python
- Import from sibling directory
- Python: import module from another directory at the same level in project hierarchy
- Cannot find the file specified when using subprocess.call(‘dir’, shell=True) in Python
- Python not able to open file with non-english characters in path
- What is wrong with my python code? [duplicate]
- how to count average sentence length (in words) from a text file contains 100 sentences using python [closed]
- What needs to be added
- Python 2.7 IndentationError [closed]
- How to detect key presses?
- Using both Python 2.x and Python 3.x in IPython Notebook
- List vs generator comprehension speed with join function [duplicate]
- Launch a completely independent process
- Why does str(float) return more digits in Python 3 than Python 2?
- Add pygame module in PyCharm IDE
- ReactorNotRestartable error in while loop with scrapy
- How to pass an operator to a python function?
- How to read Unicode input and compare Unicode strings in Python?
- Absolute imports in python not working, relative imports work
- Networkx: Overlapping edges when visualizing MultiGraph
- Mixing files and loops
- Read Outlook Events via Python
- BeautifulSoup getText from between , not picking up subsequent paragraphs
- Unknown error: Chrome failed to start: exited abnormally
- pprint dictionary on multiple lines