Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whether my file is opened in RAM while in append mode?

I have written a code which keep on append the file. Here is the code for it:

writel = open('able.csv','a',encoding='utf-8',errors='ignore')
with open('test','r',encoding='utf-8',errors='ignore') as file:
    for i in file.readlines():
        data = functionforprocess(i)
        if data is not "":
            writel.write(data)
        if count% 10000 == 0:
            log = open('log','w')
            log.write(str(count))
            log.close()

My question is: whether the file that I have opened in the append mode is available in RAM? Does that file is acting like a buffer, means If I store the data in variable and then write the variable to file is equal to open a file in append mode and write directly?

Kindly, get me out of this confusion.

like image 886
Jaffer Wilson Avatar asked Sep 04 '17 08:09

Jaffer Wilson


People also ask

Can we read file in append mode?

We can open the file in append access mode i.e. 'a', using 'with open' statement too, and then we can append the text at the end of the file.

Which mode opens the file in read and append mode?

You're looking for the r+ or a+ mode, which allows read and write operations to files (see more). With r+ , the position is initially at the beginning, but reading it once will push it towards the end, allowing you to append.

When an existing file is opened in append mode?

Write mode either overwrites the existing file or creates a new one if the file is non-existent. Append mode opens the file and sets the file pointer/cursor to the end of the file so that any write operation may start from the very end of that file. It cannot overwite an existing file.

Does opening a file use RAM?

No. As per the docs, open() wraps a system call and returns a file object, the file contents are not loaded into RAM (unless you invoke, E.G., readlines()).


1 Answers

Appending is a basic function of file I/O and is carried out by the operating system. For instance, fopen with mode a or a+ is part of the POSIX standard. With file I/O, the OS will also tend to buffer reads and writes; for instance, for most purposes it's not necessary to make sure that the data that you've passed to write is actually on the disk all the time. Sometimes it sits in a buffer somewhere in the OS; sometimes the OS dumps these buffers out to disk. You can force writes using fsync if it's important to you; this is also a really good reason to make sure that you always call close on your open file objects when you're done with them (or use a context manager); if you forget, you might get weird behaviour because of those buffers hanging around in the OS.

So, to answer your question. The file that you opened is most likely in RAM at any given moment. However, as far as I know, it's not available to you. You can interact with the data in the file using file I/O methods, but it's not like there's a buffer that you can get the memory address of, and read back what you just wrote. As to if append-mode writing is equivalent to storing something in a buffer and then writing to disk, I guess I would say no. Any kind of file I/O write will probably be buffered the same way by the OS, and the reason this is efficient is that the OS gets to make the decision on when to flush the buffers. If you store things in a variable and then write them out atomically to disk, you get to decide when the writes take place.

like image 180
wildwilhelm Avatar answered Sep 30 '22 21:09

wildwilhelm