What does `open('my_file.txt', 'r')` do if `my_file.txt` is in the same directory as the Python script?

Question

Grade: Education Subject: Support
What does `open('my_file.txt', 'r')` do if `my_file.txt` is in the same directory as the Python script?
Asked by:
103 Viewed 103 Answers

Answer (103)

Best Answer
(354)
If `my_file.txt` is in the same directory as your Python script, and your script is run from that directory, `open('my_file.txt', 'r')` will successfully open the file for reading. If the script is run from a different directory, this will likely result in a `FileNotFoundError` unless the current working directory is set to where `my_file.txt` resides.