{"id":8350,"date":"2023-11-09T18:42:31","date_gmt":"2023-11-09T13:12:31","guid":{"rendered":"https:\/\/python-programs.com\/?p=8350"},"modified":"2023-11-10T12:24:42","modified_gmt":"2023-11-10T06:54:42","slug":"python-data-persistence-file-handling-using-os-module","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/","title":{"rendered":"Python Data Persistence – File Handling using os Module"},"content":{"rendered":"
Python\u2019s built-in library has an os module that provides useful operating system-dependent functions. It also provides functions for performing low-level read\/write operations on the file. Let us briefly get acquainted with them.<\/p>\n
The open ()<\/strong> function from the os module (obviously it needs to be referred to as os. open ( ))<\/strong> is similar to the built-in open ( )<\/strong> function in the sense it also opens a file for reading\/write operations. However, it doesn\u2019t return a file or file-like object but a file descriptor, an integer corresponding to the file opened. File descriptor\u2019s values 0, 1, and 2 are reserved for stdin,<\/strong> stout<\/strong>, and stder r<\/strong> streams. Other files will be given an incremental file descriptor. Also, the write ( )<\/strong> and read( )<\/strong> functions of the os module needs bytes to object as the argument. The os .open ()<\/strong> function needs to be provided one or combinations of the following constants: (Table 5.2)<\/p>\n As in the case of a file object, the os module defines a sleek ()<\/strong> function to set file r\/w position at the desired place from the beginning, current position, or end indicated by integers 0,1, and 2 respectively.<\/p>\n Example<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Python Data Persistence – File Handling using os Module Python\u2019s built-in library has an os module that provides useful operating system-dependent functions. It also provides functions for performing low-level read\/write operations on the file. Let us briefly get acquainted with them. The open () function from the os module (obviously it needs to be referred …<\/p>\n\n\n
\n os.OWRONLY<\/td>\n open for writing only<\/td>\n<\/tr>\n \n os.ORDWR<\/td>\n open for reading and writing<\/td>\n<\/tr>\n \n os.OAPPEND<\/td>\n append on each write<\/td>\n<\/tr>\n \n os.OCREAT<\/td>\n create file if it does not exist<\/td>\n<\/tr>\n \n os.OTRUNC<\/td>\n truncate size to 0<\/td>\n<\/tr>\n \n os.OEXCL<\/td>\n error if create and file exists<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n >>> fd=os . open (\"testfile. txt\", os .0_RDWR | os .0_CREAT)\r\n>>> text=\"Hello Python\"\r\n>>> encoded=text.encode(encoding='utf-16')\r\n>>> os.write(fd, encoded)\r\n>>> os.lseek(fd,0,0)\r\n>>> encoded=os.read(fd)\r\n>>> os.path.getsizeCtestfile.txt\") #calculate file size\r\n>>> encoded=os.read(fd, 26)\r\n>>> text=encoded.decode('utf-16 ')\r\n>>> text\r\n'Hello Python'<\/pre>\n