DEV Community


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

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.

rhymes profile image

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

ddsry21 profile image
Python Programming By DDSRY Author

Thank you for sharing this✌