filmov
tv
Bangla Python Tutorial for Beginners Part 22 - Python File I/O - Read in Python Part 01

Показать описание
1. High level file reading features such as readLine, readLines, and reading the entire file using read() are actually constructed from the basic method of reading a fixed number of characters from the file then checking the content of what being read.
2. It is risky to use readLines, and read without any character count because if your program is processing a large file then the content of the whole file will be loaded into the memory and your program will consume too much memory as a result. Then in the worst case when the file is too large, the program will crush. The best strategy of file reading is to read only as much data as you need at a time and then let go of that data when you do not need it any more.
3. If you do not close a file after you are done reading/writing, the operating system level resource for file management remain occupied. There is a limit in how many such resource you can occupy in a program. So if you do not close the files and keep reading new files, then your program will crush or will be killed by the operating system.
4. Finally, if you do not close the file after you are done writing, then your changes may not be persisted in the disk as the operating system often copy data back from memory buffer to disk only during file close operation. There is another method call flush() though that you can use to force update of the disk version of the file before file close.
2. It is risky to use readLines, and read without any character count because if your program is processing a large file then the content of the whole file will be loaded into the memory and your program will consume too much memory as a result. Then in the worst case when the file is too large, the program will crush. The best strategy of file reading is to read only as much data as you need at a time and then let go of that data when you do not need it any more.
3. If you do not close a file after you are done reading/writing, the operating system level resource for file management remain occupied. There is a limit in how many such resource you can occupy in a program. So if you do not close the files and keep reading new files, then your program will crush or will be killed by the operating system.
4. Finally, if you do not close the file after you are done writing, then your changes may not be persisted in the disk as the operating system often copy data back from memory buffer to disk only during file close operation. There is another method call flush() though that you can use to force update of the disk version of the file before file close.