Why is file creating in parent folder of source code?

Issue

There is src folder witn env and main.py. I create log file like next and saw file in parent folder of src

from loguru import logger
logger.add("logs.txt")

enter image description here

In the other hand like this and file into src

dirname = path.dirname(__file__)
log_path = path.join(dirname, "logs.txt")
logger.add(log_path)

enter image description here

What is responsible to root folder when path is only filename? I need to create some files into src. I have copy-past code of dirname calculation to each of them. Looks like mistake.

Solution

Here, the problem is how the working directory is set in your IDE (VSCode).
Try to modify the working directory in the configuration:

VSCode — how to set working directory for debug.

A more generic way could be:

import os

if DEBUG:
    os.chdir('./src')

logger.add("logs.txt")

Answered By – Corralien

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published