DEV Community

DDSRY
DDSRY

Posted on • Updated on

Why is there no error when we open the file twice in Python ?

Why is there no error when you open the file twice in Python Programming Language ?

▪ Python will not restrict you from opening a file more than once, and in fact
sometimes this is necessary.

Top comments (4)

Collapse
 
davehrtng profile image
David Harting

My best guess - Athe operating system level it is valid to open a file more than once. So there isn’t a reason for Python to prevent it either.

Collapse
 
rhymes profile image
rhymes

Exactly David.

Imagine if one process had the ownership of a file descriptor for its entire lifetime. You couldn't open a file in two editors or synchronize content between a writer or a reader. You couldn't even open a file to have multiple writers. Unix pipes wouldn't probably work as well.

If you need lock everyone else out, that should definitely be an explicit request.

There's no real cost to have multiple readers so why block it by default? Locking is needed with multiple writers

Collapse
 
ddsry21 profile image
DDSRY

Thank you for sharing this✌

Collapse
 
rrdlpl profile image
R