Pergunta
How can I use `os.path.dirname(__file__)` to resolve relative paths correctly?
Perguntado por: USER1525
78 Visto
78 Respostas
Resposta (78)
`os.path.dirname(__file__)` returns the directory containing the current script. You can use it to build absolute paths based on the script's location, avoiding issues with the current working directory. For example: `script_dir = os.path.dirname(__file__); filepath = os.path.join(script_dir, 'data', 'my_file.txt')`.