A story about how to specify a relative path in python.

Also includes source code that is not directly related to the cause of the problem, as a memorandum of your own.

Event

When I try to read an ini file with python, For some reason, I get a KeyError and it doesn't work.

The source is as follows

config.ini


[USER_INFO]
address=testUser
password=testpass

test.py


import configparser
config = configparser.ConfigParser()
config.read('./config.ini', encoding='utf-8')

address = config['USER_INFO']['address']

It doesn't look like the key value is wrong.

I thought that config.ini could not be read, so I checked the existence of the file. As a result, False was returned.

python


import os

print(os.path.exists('./config.ini'))

Getting the folder wasn't working.

[Cause & Solution]

When I ran the program, I was running it from VS Code, The cause is that the current directory when executing was not the file in which test.py exists.

When I moved to the directory where test.py exists and then executed it, the value from the ini file was also successfully obtained.

Recommended Posts

A story about how to specify a relative path in python.
A story about trying to implement a private variable in Python.
How to get a stacktrace in python
I got stuck when trying to specify a relative path with relative_to () in python
[Beginner memo] How to specify the library reading path in Python
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to create a JSON file in Python
How to add a Python module search path
How to specify TLS version in python requests
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to develop in Python
How to specify a schema in Django's database settings
How to convert / restore a string with [] in python
[Python] How to expand variables in a character string
How to execute a command using subprocess in Python
[Selenium] How to specify the relative path of chromedriver?
How to write a Python class
How to slice a block multiple array from a multiple array in Python
How to collect images in Python
How to use the __call__ method in a Python class
How to use SQLite in Python
How to import a file anywhere you like in Python
How to specify a public directory Python simple HTTP server
A story about how Windows 10 users created an environment to use OpenCV3 with Python 3.5
How to define multiple variables in a python for statement
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]
How to specify Cache-Control for blob storage in Azure Storage in Python
How to get the last (last) value in a list in Python
How to use Mysql in python
How to get a list of built-in exceptions in python
How to wrap C in Python
A story about how to deal with the CORS problem
How to use ChemSpider in Python
How to use PubChem in Python
A story about trying to introduce Linter in the middle of a Python (Flask) project
Data science companion in python, how to specify elements in pandas
How to handle Japanese in Python
[Note] A story about trying to override a class method with two underscores in Python 3 series.
How to determine the existence of a selenium element in Python
How to pass arguments to a Python script in SPSS Modeler Batch
How to make a string into an array or an array into a string in Python
How about Anaconda for building a machine learning environment in Python?
A story about adding a REST API to a daemon made with Python
[Introduction to Python] How to output a character string in a Print statement
How to check the memory size of a variable in Python
How to get a string from a command line argument in python
How to create a heatmap with an arbitrary domain in Python
[Introduction to Python] How to use the in operator in a for statement?
How to specify a .py file to load at startup in IPython 0.13
How to check the memory size of a dictionary in Python
[For beginners] How to register a library created in Python in PyPI
A story about trying to run multiple python versions (Mac edition)
Read a file in Python with a relative path from the program
How to delete multiple specified positions (indexes) in a Python list
[Introduction to Python] How to use class in Python?
How to dynamically define variables in Python
[Python] How to make a class iterable
How to do R chartr () in Python