{"flag":true,"single":true,"pageTitle":"Python File Handling Readfile, write to file, delete files , CRUD Files handling in python","post":{"id":50,"user_id":"1","slug":"python-file-handling-readfile-write-to-file-delete-files-crud-files-handling-in-python-lp0u","title":"Python File Handling Readfile, write to file, delete files , CRUD Files handling in python","body":"<p><strong>open()<\/strong> is used to handle files<\/p>\r\n<ul>\r\n<li>\"r\" - Read - Default value. Opens a file for reading, error if the file does not exist<\/li>\r\n<li>\"a\" - Append - Opens a file for appending, creates the file if it does not exist<\/li>\r\n<li>\"w\" - Write - Opens a file for writing, creates the file if it does not exist<\/li>\r\n<li>\"x\" - Create - Creates the specified file, returns an error if the file exists<\/li>\r\n<li>\"b\" - Binary - Binary mode (e.g. images)<\/li>\r\n<li>\"t\" - Text - Default value. Text mode<\/li>\r\n<\/ul>\r\n<p><strong>1.Read Whole File<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>f = open(\"demofile.txt\", \"r\") #\"D:\\\\myfiles\\welcome.txt\"\r\nprint(f.read())<\/code><\/pre>\r\n<p><strong>2. Read Part of file ie first 5 charters<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>print(f.read(5))<\/code><\/pre>\r\n<p><strong>3. Read Line from file<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>f = open(\"demofile.txt\", \"r\")\r\nprint(f.readline()) #read first line \r\nprint(f.readline()) # read 2nd line<\/code><\/pre>\r\n<p><strong>4. Read file line by line<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>f = open(\"demofile.txt\", \"r\")\r\nfor x in f:\r\n  print(x)<\/code><\/pre>\r\n<p><strong>5.close file<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>f.close()<\/code><\/pre>\r\n<p><strong>WRITE TO FILES<\/strong><\/p>\r\n<p><strong>1. Append to file<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>f = open(\"demofile2.txt\", \"a\")\r\nf.write(\"Now the file has more content!\")\r\nf.close()<\/code><\/pre>\r\n<p>It will open files and append the new content<\/p>\r\n<p><strong>2. overwrite to file<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>f = open(\"demofile3.txt\", \"w\")\r\nf.write(\"Woops! I have deleted the content!\")\r\nf.close()<\/code><\/pre>\r\n<p><strong>DELETE FILE<\/strong><\/p>\r\n<p><strong>1. It will remove the file<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>import os\r\nos.remove(\"demofile.txt\")<\/code><\/pre>\r\n<p><strong>2. check if file exist then delete it<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>import os\r\nif os.path.exists(\"demofile.txt\"):\r\n  os.remove(\"demofile.txt\")\r\nelse:\r\n  print(\"The file does not exist\")<\/code><\/pre>\r\n<p><strong>3.Delete the folder<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>import os\r\nos.rmdir(\"myfolder\")<\/code><\/pre>","category_id":"5","is_private":"0","created_at":"2023-03-26T05:52:37.000000Z","updated_at":"2023-03-26T05:52:37.000000Z","category":{"id":5,"user_id":"1","name":"Python","slug":"python-6gqr","parent_id":null,"created_at":"2023-03-16T12:36:56.000000Z","updated_at":"2023-03-16T12:36:56.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"open() is used to handle files  \"r\" - Read - Default value. Opens a file for reading, error if the file does not exist \"a\" - Append - Opens  - Python File Handling Readfile, write to file, delete files , CRUD Files handling in python (Updated: March 26, 2023) - Read more about Python File Handling Readfile, write to file, delete files , CRUD Files handling in python at my programming site [SITE]","categories":[]}