[PYTHON] How to read a file in a different directory

Purpose

I want to read a file in a different directory

Method

Add the following code to __init__.py under the directory where the file you want to read is located.

import os, sys
sys.path.append(os.path.abspath(os.path.dirname(__file__)))

Recommended Posts

How to read a file in a different directory
How to read a CSV file with Python 2/3
How to create a JSON file in Python
How to read a serial number file in a loop, process it, and graph it
How to create a config file
How to import a file anywhere you like in Python
How to get a stacktrace in python
How to read CSV files in Pandas
How to make a command to read the configuration file with pyramid
Save the Pydrive authentication file in a different directory from the script
How to specify a .py file to load at startup in IPython 0.13
[Django] How to read variables / constants defined in an external file
[Ln] How to paste a symbolic link in a directory is complicated
How to disguise a ZIP file as a PNG file
How to embed a variable in a python string
Read a file containing garbled lines in Python
[Python] How to read excel file with pandas
How to specify a .ui file in the dialog / widget GUI in PySide
How to read JSON
How to implement a gradient picker in Houdini
[Python] How to read a csv file (read_csv method of pandas module)
How to notify a Discord channel in Python
I read "How to make a hacking lab"
How to read time series data in PyTorch
How to create a Rest Api in Django
How to write a named tuple document in 2020
How to read environment variables from .env file in PyCharm (on Mac)
How to count numbers in a specific range
How to Mock a Public function in Pytest
Various ways to read the last line of a csv file in Python
How to read all the classes contained in * .py in the directory specified by Python
How to drop Google Docs in one folder in a .txt file with python
How to get a list of files in the same directory with python
Read the Python-Markdown source: How to create a parser
How to specify a schema in Django's database settings
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python
How to know the current directory in Python in Blender
[Python] How to expand variables in a character string
A memorandum on how to use keras.preprocessing.image in Keras
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
How to use template engine in pyramid 1 file application
How to display DataFrame as a table in Markdown
How to turn a .py file into an .exe file
How to convert a mel spectrogram back to a wav file
How to change static directory from default in Flask
[Work efficiency] How to change file names in Python
How to execute a command using subprocess in Python
Output a binary dump in binary and revert to a binary file
2 ways to read all csv files in a folder
How to read csv containing only integers in Python
[Linux] How to put your IP in a variable
How to call a function
Upload a file to Dropbox
Read and write a file
How to hack a terminal
Write and read a file
How to develop in Python
How to display the modification date of a file in C language up to nanoseconds
How to list files under the specified directory in a list (multiple conditions / subdirectory search)