Read a file in Python with a relative path from the program

The path of the executable file can be obtained with __file__, so use ʻos.path.dirname to make it a directory. Use ʻos.path.join to connect the path to the file you want.

.
└── some_dir
    ├── products.json
    └── test.py

test.py


import os
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'products.json')
with open(filename) as f
    print(f.read())

Recommended Posts

Read a file in Python with a relative path from the program
Access the file with a relative path from the execution script.
Read line by line from a file with Python
Run a Python file with relative import in PyCharm
Let's read the RINEX file with Python ①
Read the file line by line in Python
Read the file line by line in Python
[Python] Read the specified line in the file
How to read a CSV file with Python 2/3
[Python] Get the files in a folder with Python
Read table data in PDF file with Python
Extract lines that match the conditions from a text file with python
Read a Python # .txt file for a super beginner in Python with a working .py
I made a program to check the size of a file in Python
[Implementation example] Read the file line by line with Cython (Python) from the last line
Various ways to read the last line of a csv file in Python
Download the file in Python
Receive dictionary data from a Python program in AppleScript
Output the time from the time the program was started in python
Python --Read data from a numeric data file and find the multiple regression line.
Try embedding Python in a C ++ program with pybind11
Read QR code from image file with Python (Mac)
From buying a computer to running a program with python
Replace the named entity in the read text file with a label (using GiNZA)
[Python] Create a Tkinter program distribution file with cx_Freeze
Get the value of a specific key in a list from the dictionary type in the list with Python
I got stuck when trying to specify a relative path with relative_to () in python
Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_
[Python] Create a file & folder path specification screen with tkinter
A story about how to specify a relative path in python.
Change the standard output destination to a file in Python
[Python] Read the csv file and display the figure with matplotlib
[Note] Import of a file in the parent directory in Python
Python> Read from a multi-line string instead of a file> io.StringIO ()
Extract the xz file with python
File / folder path manipulation in Python
Save the binary file in Python
Get the desktop path in Python
Get the script path in Python
Read files in parallel with Python
Get the desktop path in Python
Read fbx from python with cinema4d
When writing a program in Python
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
Process the contents of the file in order with a shell script
How to make a command to read the configuration file with pyramid
Save the Pydrive authentication file in a different directory from the script
The idea of feeding the config file with a python file instead of yaml
Read the config file in Go language! Introducing a simple sample
[Python] Read a csv file with a large data size using a generator
Get the formula in an excel file as a string in Python
Solve the subset sum problem with a full search in Python
Get a list of files in a folder with python without a path
Create a compatibility judgment program with the random module of python.
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
[Python] A program that creates stairs with #
Exclusive control with lock file in Python
[Note] Read a file from another directory
[Python] Create a program to delete line breaks in the clipboard + Register as a shortcut with windows
Read CSV file with python (Download & parse CSV file)
I made a payroll program in Python!