open() function error

Asked 2 years ago, Updated 2 years ago, 97 views

Recently, I started studying Python on my own.
I'm still so immature that I can't even call you a beginner, but I'm sorry to ask you this question.

I use Eclipse.
I wrote the following code in the open() function.

open_file=open('point.txt')
raw_data=open_file.read()
open_file.close()
print(raw_data)

point.txt is in the same folder, and the results of the loading are displayed when running in the Eclipse app, but when you run it at the command prompt,

open_file=open('point.txt')
FileNotFoundError: [Errno2] No such file or directory: 'point.txt'

The file does not exist.
Is the Eclipse setting not working?

Sorry for the rudimentary question.
By the way, Eclipse uses PyDev.

python eclipse

2022-09-30 21:34

2 Answers

If you specify the current directory in cd and run it, it will open.


2022-09-30 21:34

If you put a full pass in the first line as shown below, it will work wherever the prompt is.
Also, the prefix 'r' or 'R' treats the backslash as literal characters.
open_file=open(r'D:\abc\efg\point.txt')


2022-09-30 21:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.