With this type of thing you need to be careful what your actual working directory is. For example, you may not run the script from the directory the file is in. In this case, you can’t just use a relative path by itself.
If you are sure the file you want is in a subdirectory beneath where the script is actually located, you can use __file__
to help you out here. __file__
is the full path to where the script you are running is located.
So you can fiddle with something like this:
import os script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in rel_path = "2091/data.txt" abs_file_path = os.path.join(script_dir, rel_path)
Related Posts:
- Relative paths in Python
- How to get an absolute file path in Python
- Python list directory, subdirectory, and files
- Why am I getting a FileNotFoundError?
- Relative paths in Python
- Relative paths in Python
- How do I copy a file in Python?
- How do I copy a file in Python?
- How to open a file using the open with statement
- Using “with open() as file” method, how to write more than once? [duplicate]
- How to read a file line-by-line into a list?
- TypeError: a bytes-like object is required, not ‘str’ when writing to a file in Python3
- Confused by python file mode “w+”
- File open and close in python
- Do I understand os.walk right?
- FileNotFoundError: [Errno 2] No such file or directory [duplicate]
- How to move a file in Python?
- Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory
- FileNotFoundError: [Errno 2] No such file or directory
- How to move a file in Python?
- How do you append to a file?
- Importing modules from parent folder
- Importing modules from parent folder
- Writing a list to a file with Python
- Why do I get “Pickle – EOFError: Ran out of input” reading an empty file?
- How do I copy a file in Python?
- ImportError: DLL load failed: %1 is not a valid Win32 application. But the DLL’s are there
- ImportError: DLL load failed: %1 is not a valid Win32 application. But the DLL’s are there
- Python – from . import
- How do I check whether a file exists without exceptions?
- Call a function from another file?
- How to import files in python using sys.path.append?
- Python Setup Disabling Path Length Limit Pros and Cons?
- Writing a list to a file with Python
- How can I check file size in Python?
- Where does this come from: -*- coding: utf-8 -*-
- Python: Write array values into file
- Importing variables from another file?
- How to copy files?
- Flask raises TemplateNotFound error even though template file exists
- importing external “.txt” file in python
- Search and replace a line in a file in Python
- How to save a dictionary to a file?
- How to open a file for both reading and writing?
- How do you read a file into a list in Python?
- SSL error : routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
- What is the perfect counterpart in Python for “while not EOF”
- python is not recognized windows 10
- Windows path in Python
- .write not working in Python
- Creating files and directories via Python
- How to convert a file into a dictionary?
- How to delete a specific line in a file?
- Python read in string from file and split it into values
- How to change pip installation path
- How often does python flush to a file?
- How to open every file in a folder
- TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper
- Loading and parsing a JSON file with multiple JSON objects
- How to find out whether a file is at its `eof`?
- Python IOError: File not open for reading
- Replace ‘ with \’ in a string
- How can I install pip on Windows?
- Best way to convert string to bytes in Python 3?
- Does Python have a string ‘contains’ substring method?
- Difference between import numpy and import numpy as np
- No module named ‘sklearn.cross_validation’
- Get Color Palettes from ColorHunt.co in Python
- What does the Pydoc module do?
- size of NumPy array
- Best way to strip punctuation from a string
- How can I explicitly free memory in Python?
- filename.whl is not supported wheel on this platform
- What is the meaning of [:] in python [duplicate]
- Split by comma and strip whitespace in Python
- How to remove punctuation marks from a string in Python 3.x using .translate()?
- Calculating Covariance with Python and Numpy
- What is the easiest way to remove all packages installed by pip?
- In practice, what are the main uses for the new “yield from” syntax in Python 3.3?
- How can I install Python’s pip3 on my Mac?
- Error handling in Python-MySQL
- List files ONLY in the current directory
- No Module Named ‘_pywrap_tensorflow_internal’
- Python: AttributeError: ‘_io.TextIOWrapper’ object has no attribute ‘split’
- How to detect key presses?
- isPrime Function for Python Language
- Python script to copy text to clipboard
- ImportError: No module named cv2
- ./xx.py: line 1: import: command not found
- Issue with virtualenv – cannot activate
- Can I set max_retries for requests.request?
- Why do I get this error “TypeError: ‘method’ object is not iterable”?
- Are nested try/except blocks in Python a good programming practice?
- Getting a name error when trying to input a string [duplicate]
- Complex number troubles with numpy
- Running shell command and capturing the output
- Python 3 integer division [duplicate]
- numpy/scipy/ipython:Failed to interpret file as a pickle
- What is the Python equivalent of static variables inside a function?
- Python – difference between two strings