I have a question regarding Python's open function.

Asked 1 years ago, Updated 1 years ago, 128 views

1. Is the open function and the open method of the io module the same?

Or are they similar functions but separate functions?

2. When opening a file with an open function and reading the contents of the file with read(),


 f = open('a.txt', 'r')

 s =  f.read()

When read() is executed in the above code,

Do you actually access and read the a.txt file stored on the hard disk?

Alternatively, when open() is executed, the contents of the a.txt file stored on the hard disk are

fIt is contained in an object and stored somewhere on the memory,

If read() is then executed,

fAccess the object and store it in memory (not on the hard disk)

I wonder if I'm going to read the contents of the file.

python open

2022-09-22 10:45

1 Answers

I think I need to learn about the io stream.

Python is difficult. It's so abstracted that it's easy to handle, not easy.

Opening does not mean that the contents of the file are saved in memory. It's literally opening up a file, that is, it's easy to act like a pointer to the file.

Saving to physical memory is when calling the read flow method.

I recommend you to learn the file IO with c first.

If you use functions like fopen, fgets, and seek, you can see more specifically what it looks like when you read it from a file, and you can see how abstract Python is.


2022-09-22 10:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.