Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is time complexity of Python3's open() function?

What is the time complexity in Big O notation of Python3's open() function when appending to a file?

For example, the following line: with open("HugeDocument.csv", "a") as f

like image 270
Tyler Collins Avatar asked Jul 27 '26 22:07

Tyler Collins


1 Answers

Based on my understanding the open() function returns a file object which is a pointer/handle to the actual resource on the disk. Hence the complexity of open() should be constant, since the location of the file is passed into the open(file='abc') function.

https://docs.python.org/3/glossary.html#term-file-object

An object exposing a file-oriented API (with methods such as read() or write()) to an underlying resource. Depending on the way it was created, a file object can mediate access to a real on-disk file or to another type of storage or communication device (for example standard input/output, in-memory buffers, sockets, pipes, etc.).

Performing operations using the file object will have different complexity depending on the type of operation (like read(), readline(), seek()), the file size, system memory limits and other File System configurations.

like image 163
Shishir Avatar answered Jul 30 '26 13:07

Shishir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!