Tuesday 29 April 2014

Importance of closing file descriptor

Hi all,
Today I am going to show you the importance of closing file descriptor. So when you last time wrote fp.close() remember? Not a problem (from now onward :P)! Because I will tell you what might happen to the file if you don't close it's descriptor.

This sudden knowledge came to me after trying to solve a bug for more than an hour! I was trying to write a file and I didn't close it's descriptor and the next thing I was doing is using the same updated file for another function. But what happened was total tragedy! I will tell you the reason afterwards first, let me tell you what happened to me!

So I wrote one function which opens a file in python language and then I am going to use the same file (not same file descriptor) in shell command. So guys what happened is the shell command knew that file exists there but it was not able to read the content of the file. And to check if there is some error/bug or not I unit tested both the things! They all were fine! So my fuckking question was why is it they are not working properly when integrated!!????


The reason was simple when you close your file descriptor during your program execution or your program terminated then only content editing/removal will happen otherwise content will not be written in the  file ! so guys be careful otherwise it might be hectic to solve a miserable and hidden bugs :)

1 comment: