0

Why Files have to be opened before they can be accessed

In python you have to use the open() function to open a file before the file can be futher operated. What are the advantages of this? For example does the open function move a file to RAM? In the same way the close method would remove the file from RAM. Without the open function python should just decide how long it will keep a file in RAM? So the open function gives programmer more flexibility because she can decide when files are needed and in what mode they will be operated.

12th Aug 2017, 10:21 AM
Oula Magga
Oula Magga - avatar
5 Respostas
0
The point of opening or creating files is self explanatory. You either want to access external data, or you want to save data to an external file outside the script. If you just use: open ("file.txt", "r") it does nothing and doesn't store anything. Python would open the file and then nothing. You need to store the information inside into a variable in order to manipulate it. The moment you store the file, you stored it into system memory. The open function isn't limited to just opening but creating new files, if one doesn't already exist. It depends what command argument you give it. example: file = open("test.txt", "r") Here you told python, open this file, then store it into the variable file as a file object. You can use methods on this file object to extract it's data and do whatever you wish to do with it. There is also "a" which stands for append, and "w" which stands for write to file. "r" means to read only.
12th Aug 2017, 11:28 AM
Sapphire
0
It's only logical. In order to know what is inside a file, the computer must open it - hence the open () function. Nothing is stored from inside the moment it is opened until you start grabbing information from it and storing it in variables ... in turn storing it on python's heap, and into system memory for as long as there is a reference to that data. If there are no longer any reference to something, pythons garbage collector forgets it. For example: a = 4 Python stores A on something called a frame, and then it is referred (pointed) towards 4 on the heap. Think of the heap as a type of storage, and frames a bunch of labels that refer to data on the heap. if you lets say: a = 5 a in the frame will now reference to 5 that is stored in the heap container. The 4, since nothing on the frame refers to it, is forgotten. This is a simple example and the topic is sort of complex. The 5 is also a simple example. The heap actually has predefined integers that are commonly used, for better performance. maybe someday I'll write a tutorial for people on it at some point, if people even care about that. lol
12th Aug 2017, 10:27 AM
Sapphire
0
Ok so I was wrong with the storing advantage. Your answer doesn't explain what it means in python to open a file. To me opening a file means nothing and sounds irrelevant. Or you mean that basicly a file is stored to RAM when I execute myFile = open("myFile.txt")?
12th Aug 2017, 11:13 AM
Oula Magga
Oula Magga - avatar
0
Ok now I understand. Opening a file means in python to create an file object. In OOP every data is intepreted as objects except basic data types like int or float. In this OOP sense the open function is obvious.
12th Aug 2017, 11:39 AM
Oula Magga
Oula Magga - avatar
0
Yes, keep in mind to always use the close() method after you're finished with the file to remove it from system memory. ex: file.close() People who claim there is no need for this, do not understand.
12th Aug 2017, 11:42 AM
Sapphire