-1

I launch my py file from C:\Users\UserDirectory\OneDrive\Bureau\python. But when I want to load a file saved in python directory with the open() method, so in the same directory that my py file, python return the FileNotFoundError.

For example:

filename = 'test.txt'
with open(filename, 'r') as f:
    lines = f.read()

I guess it's very simple problem to resolve but I could'nt by myself. I noticed that when I use write(), it saves file in C:\Users\UserDirectory. thanks for help

noGodHere
  • 1
  • 2
  • How are you launching your file? Python resolves paths relatively to current working directory, not where the file is located. To make sure the path of the file is used instead, you can `from pathlib import Path` and do `Path(__file__).parent / filename`. – DallogFheir Aug 21 '23 at 18:13

1 Answers1

0

This is a similar issue to yours. You could try some of the solutions mentioned there.