{"id":482,"date":"2025-05-19T11:38:34","date_gmt":"2025-05-19T11:38:34","guid":{"rendered":"https:\/\/buhave.com\/courses\/?p=482"},"modified":"2025-05-20T12:56:49","modified_gmt":"2025-05-20T12:56:49","slug":"file-handling","status":"publish","type":"post","link":"https:\/\/buhave.com\/courses\/python\/file-handling\/","title":{"rendered":"File Handling"},"content":{"rendered":"<h2>Reading and writing files<\/h2>\n<h3>What Is File Handling?<\/h3>\n<p>File handling lets you read from, write to, and modify files stored on your computer. Python uses the built-in open() function to interact with files.<\/p>\n<p><strong>Opening a File<\/strong><\/p>\n<p style=\"text-align: center\"><em>file = open(&#8220;example.txt&#8221;, &#8220;mode&#8221;)<\/em><\/p>\n<p><strong>Modes:<\/strong><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n<thead>\n<tr>\n<th><strong>Mode<\/strong><\/th>\n<th><strong>Description<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>'r'<\/code><\/td>\n<td>Read (default mode)<\/td>\n<\/tr>\n<tr>\n<td><code>'w'<\/code><\/td>\n<td>Write (overwrites existing file)<\/td>\n<\/tr>\n<tr>\n<td><code>'a'<\/code><\/td>\n<td>Append to end of file<\/td>\n<\/tr>\n<tr>\n<td><code>'x'<\/code><\/td>\n<td>Create a new file (error if file already exists)<\/td>\n<\/tr>\n<tr>\n<td><code>'b'<\/code><\/td>\n<td>Binary mode (e.g., <code>'rb'<\/code>, <code>'wb'<\/code>)<\/td>\n<\/tr>\n<tr>\n<td><code>'t'<\/code><\/td>\n<td>Text mode (default)<\/td>\n<\/tr>\n<tr>\n<td><code>'+'<\/code><\/td>\n<td>Read and write (updates existing file)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Reading from a File<\/strong><\/p>\n<p><strong>Read Entire File<\/strong><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;example.txt&#8221;, &#8220;r&#8221;) as f:<\/em><br \/>\n<em>content = f.read()<\/em><br \/>\n<em>print(content)<\/em><\/p>\n<p><strong>Read Line-by-Line<\/strong><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;example.txt&#8221;, &#8220;r&#8221;) as f:<\/em><br \/>\n<em>lines = f.readlines()<\/em><\/p>\n<p><strong>Writing to a File<\/strong><\/p>\n<p><strong>Overwrite (Write Mode &#8216;w&#8217;)<\/strong><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;output.txt&#8221;, &#8220;w&#8221;) as f:<\/em><br \/>\n<em>f.write(&#8220;Hello, world!\\n&#8221;)<\/em><\/p>\n<p><strong>Append (Append Mode &#8216;a&#8217;)<\/strong><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;output.txt&#8221;, &#8220;a&#8221;) as f:<\/em><br \/>\n<em>f.write(&#8220;This line will be added.\\n&#8221;)<\/em><\/p>\n<p><strong>Writing Multiple Lines<\/strong><\/p>\n<p style=\"text-align: center\"><em>lines = [&#8220;Line 1\\n&#8221;, &#8220;Line 2\\n&#8221;, &#8220;Line 3\\n&#8221;]<\/em><br \/>\n<em>with open(&#8220;lines.txt&#8221;, &#8220;w&#8221;) as f:<\/em><br \/>\n<em>f.writelines(lines)<\/em><\/p>\n<p><strong>Always Use with<\/strong><\/p>\n<p>The with statement automatically closes the file when done (safer and cleaner than manually using file.close()).<\/p>\n<p><strong>Common Errors<\/strong><\/p>\n<ul>\n<li>FileNotFoundError: File does not exist in read mode.<\/li>\n<li>PermissionError: You don\u2019t have permission to access\/write.<\/li>\n<li>UnicodeDecodeError: Mismatch in encoding.<\/li>\n<\/ul>\n<p><strong>To fix encoding issues:<\/strong><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;file.txt&#8221;, encoding=&#8221;utf-8&#8243;) as f:<\/em><br \/>\n<em>&#8230;<\/em><\/p>\n<p><strong>Reading\/Writing Binary Files<\/strong><\/p>\n<p>For non-text files (images, PDFs, etc.):<\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;image.jpg&#8221;, &#8220;rb&#8221;) as f:<\/em><br \/>\n<em>data = f.read()<\/em><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;copy.jpg&#8221;, &#8220;wb&#8221;) as f:<\/em><br \/>\n<em>f.write(data)<\/em><\/p>\n<p><strong>Summary:<\/strong><\/p>\n<ul>\n<li>Use open(filename, mode) to access files.<\/li>\n<li>Use read(), readline(), or readlines() to read.<\/li>\n<li>Use write() or writelines() to write.<\/li>\n<li>Always use with open(&#8230;) for safe file handling.<\/li>\n<li>Choose modes (r, w, a, b, etc.) based on your need.<\/li>\n<\/ul>\n<h2>with statement<\/h2>\n<h3>What Is the with Statement?<\/h3>\n<p>The with statement simplifies resource management by ensuring that setup and cleanup actions (like opening and closing a file) are automatically handled \u2014 even if errors occur.<\/p>\n<p>It&#8217;s most commonly used for file handling, but applies to any object that supports a &#8220;context manager&#8221; (i.e., has __enter__() and __exit__() methods).<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<p style=\"text-align: center\"><em>with expression as variable:<\/em><br \/>\n<em># do something with variable<\/em><\/p>\n<p><strong>Example (file handling):<\/strong><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;example.txt&#8221;, &#8220;r&#8221;) as f:<\/em><br \/>\n<em>content = f.read()<\/em><br \/>\n<em>print(content)<\/em><br \/>\n<em># File is automatically closed after this block<\/em><\/p>\n<p><strong>Behind the Scenes<\/strong><\/p>\n<p><strong>When you use:<\/strong><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;file.txt&#8221;) as f:<\/em><br \/>\n<em>&#8230;<\/em><\/p>\n<p><strong>It&#8217;s roughly equivalent to:<\/strong><\/p>\n<p style=\"text-align: center\"><em>f = open(&#8220;file.txt&#8221;)<\/em><br \/>\n<em>try:<\/em><br \/>\n<em># Do something<\/em><br \/>\n<em>finally:<\/em><br \/>\n<em>f.close()<\/em><\/p>\n<p>But shorter, cleaner, and error-safe.<\/p>\n<p><strong>Benefits of with<\/strong><\/p>\n<ul>\n<li>Automatic cleanup: No need to call close() manually.<\/li>\n<li>Error handling: Ensures proper exit even if an exception occurs.<\/li>\n<li>Cleaner code: Less boilerplate, more readable.<\/li>\n<li>Prevents resource leaks: Great for files, sockets, database connections, etc.<\/li>\n<\/ul>\n<p><strong>Multiple Contexts<\/strong><\/p>\n<p><strong>You can handle multiple resources in one line:<\/strong><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;in.txt&#8221;, &#8220;r&#8221;) as fin, open(&#8220;out.txt&#8221;, &#8220;w&#8221;) as fout:<\/em><br \/>\n<em>for line in fin:<\/em><br \/>\n<em>fout.write(line.upper())<\/em><\/p>\n<p><strong>Custom Context Managers<\/strong><\/p>\n<p><strong>You can make your own objects usable with with by defining:<\/strong><\/p>\n<p style=\"text-align: center\"><em>class MyContext:<\/em><br \/>\n<em>def __enter__(self):<\/em><br \/>\n<em>print(&#8220;Entered&#8221;)<\/em><br \/>\n<em>return self<\/em><\/p>\n<p><em>def __exit__(self, exc_type, exc_value, traceback):<\/em><br \/>\n<em>print(&#8220;Exited&#8221;)<\/em><\/p>\n<p style=\"text-align: center\"><em>with MyContext() as obj:<\/em><br \/>\n<em>print(&#8220;Inside block&#8221;)<\/em><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p style=\"text-align: center\"><em>Entered<\/em><br \/>\n<em>Inside block<\/em><br \/>\n<em>Exited<\/em><\/p>\n<p><strong>Common Use Cases<\/strong><\/p>\n<ul>\n<li>File handling (open)<\/li>\n<li>Database connections (sqlite3.connect)<\/li>\n<li>Thread\/lock management (with threading.Lock():)<\/li>\n<li>Temporary files and directories<\/li>\n<li>Network sockets<\/li>\n<li>Custom cleanup routines<\/li>\n<\/ul>\n<p><strong>Summary:<\/strong><\/p>\n<ul>\n<li>The with statement manages setup\/teardown automatically.<\/li>\n<li>It&#8217;s used with context managers to write safer, cleaner code.<\/li>\n<li>Most useful for files, but works for many other resources.<\/li>\n<li>Saves time and avoids bugs caused by forgetting to release a resource.<\/li>\n<\/ul>\n<h2>Working with CSV and JSON files<\/h2>\n<h3>CSV (Comma-Separated Values)<\/h3>\n<p>CSV files store tabular data as plain text, with each row on a new line and each column separated by commas.<\/p>\n<p><strong>Reading a CSV File<\/strong><\/p>\n<p><strong>Python provides the built-in csv module:<\/strong><\/p>\n<p style=\"text-align: center\"><em>import csv<\/em><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;data.csv&#8221;, newline=&#8221;) as file:<\/em><br \/>\n<em>reader = csv.reader(file)<\/em><br \/>\n<em>for row in reader:<\/em><br \/>\n<em>print(row)<\/em><\/p>\n<p><strong>Writing to a CSV File<\/strong><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;output.csv&#8221;, &#8220;w&#8221;, newline=&#8221;) as file:<\/em><br \/>\n<em>writer = csv.writer(file)<\/em><br \/>\n<em>writer.writerow([&#8220;Name&#8221;, &#8220;Age&#8221;])<\/em><br \/>\n<em>writer.writerow([&#8220;Alice&#8221;, 30])<\/em><\/p>\n<p><strong> Reading as Dictionary<\/strong><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;data.csv&#8221;) as file:<\/em><br \/>\n<em>reader = csv.DictReader(file)<\/em><br \/>\n<em>for row in reader:<\/em><br \/>\n<em>print(row[&#8220;Name&#8221;], row[&#8220;Age&#8221;])<\/em><\/p>\n<p><strong>Writing with DictWriter<\/strong><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;output.csv&#8221;, &#8220;w&#8221;, newline=&#8221;) as file:<\/em><br \/>\n<em>fieldnames = [&#8220;Name&#8221;, &#8220;Age&#8221;]<\/em><br \/>\n<em>writer = csv.DictWriter(file, fieldnames=fieldnames)<\/em><br \/>\n<em>writer.writeheader()<\/em><br \/>\n<em>writer.writerow({&#8220;Name&#8221;: &#8220;Bob&#8221;, &#8220;Age&#8221;: 25})<\/em><\/p>\n<p><strong>Common CSV Tips<\/strong><\/p>\n<ul>\n<li>Use newline=&#8221; on Windows to avoid blank lines.<\/li>\n<li>Customize the delimiter using csv.reader(file, delimiter=&#8217;;&#8217;).<\/li>\n<\/ul>\n<h3>JSON (JavaScript Object Notation)<\/h3>\n<p>JSON is a lightweight format for structured data, commonly used in APIs and configurations.<\/p>\n<p><strong>Python Data \u2194 JSON<\/strong><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n<thead>\n<tr>\n<th>Python<\/th>\n<th>JSON<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>dict<\/code><\/td>\n<td>object<\/td>\n<\/tr>\n<tr>\n<td><code>list<\/code> \/ <code>tuple<\/code><\/td>\n<td>array<\/td>\n<\/tr>\n<tr>\n<td><code>str<\/code><\/td>\n<td>string<\/td>\n<\/tr>\n<tr>\n<td><code>int<\/code> \/ <code>float<\/code><\/td>\n<td>number<\/td>\n<\/tr>\n<tr>\n<td><code>True<\/code> \/ <code>False<\/code><\/td>\n<td><code>true<\/code> \/ <code>false<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>None<\/code><\/td>\n<td><code>null<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Reading JSON from a File<\/strong><\/p>\n<p style=\"text-align: center\"><em>import json<\/em><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;data.json&#8221;, &#8220;r&#8221;) as file:<\/em><br \/>\n<em>data = json.load(file)<\/em><br \/>\n<em>print(data)<\/em><\/p>\n<p><strong>Writing JSON to a File<\/strong><\/p>\n<p style=\"text-align: center\"><em>with open(&#8220;output.json&#8221;, &#8220;w&#8221;) as file:<\/em><br \/>\n<em>json.dump(data, file, indent=4)<\/em><\/p>\n<p><strong>Converting Between JSON Strings and Python<\/strong><\/p>\n<p style=\"text-align: center\"><em>json_string = &#8216;{&#8220;name&#8221;: &#8220;Alice&#8221;, &#8220;age&#8221;: 30}&#8217;<\/em><br \/>\n<em>data = json.loads(json_string) # JSON \u2192 Python dict<\/em><\/p>\n<p style=\"text-align: center\"><em>new_json = json.dumps(data, indent=2) # Python \u2192 JSON string<\/em><br \/>\n<em>print(new_json)<\/em><\/p>\n<p><strong>Pretty Printing JSON<\/strong><\/p>\n<p style=\"text-align: center\"><em>print(json.dumps(data, indent=4, sort_keys=True))<\/em><\/p>\n<p><strong>Summary:<\/strong><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n<thead>\n<tr>\n<th><strong>Feature<\/strong><\/th>\n<th><strong>CSV<\/strong><\/th>\n<th><strong>JSON<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Type<\/td>\n<td>Flat, tabular data<\/td>\n<td>Nested, structured data<\/td>\n<\/tr>\n<tr>\n<td>Module<\/td>\n<td><code>csv<\/code><\/td>\n<td><code>json<\/code><\/td>\n<\/tr>\n<tr>\n<td>Read<\/td>\n<td><code>csv.reader<\/code>, <code>csv.DictReader<\/code><\/td>\n<td><code>json.load()<\/code><\/td>\n<\/tr>\n<tr>\n<td>Write<\/td>\n<td><code>csv.writer<\/code>, <code>csv.DictWriter<\/code><\/td>\n<td><code>json.dump()<\/code><\/td>\n<\/tr>\n<tr>\n<td>Use<\/td>\n<td>Excel, spreadsheets, data exports<\/td>\n<td>APIs, configs, data interchange<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Let me know if you\u2019d like examples using Pandas, or want a hands-on challenge to convert CSV to JSON and vice versa!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reading and writing files What Is File Handling? File handling lets you read from, write to, and modify files stored on your computer. Python uses the built-in open() function to interact with files. Opening a File file = open(&#8220;example.txt&#8221;, &#8220;mode&#8221;) Modes: Mode Description &#8216;r&#8217; Read<\/p>\n","protected":false},"author":1,"featured_media":483,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-482","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>File Handling - Python Course<\/title>\n<meta name=\"description\" content=\"File handling in Python involves opening, reading, writing, and managing files efficiently using built-in functions and context managers.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/buhave.com\/courses\/python\/file-handling\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"File Handling - Python Course\" \/>\n<meta property=\"og:description\" content=\"File handling in Python involves opening, reading, writing, and managing files efficiently using built-in functions and context managers.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/buhave.com\/courses\/python\/file-handling\/\" \/>\n<meta property=\"og:site_name\" content=\"BUHAVE\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BeYouHave\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/naveedsafdarawan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-19T11:38:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-20T12:56:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/File-Handling.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Naveed Safdar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Naveed Safdar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/file-handling\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/file-handling\\\/\"},\"author\":{\"name\":\"Naveed Safdar\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\"},\"headline\":\"File Handling\",\"datePublished\":\"2025-05-19T11:38:34+00:00\",\"dateModified\":\"2025-05-20T12:56:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/file-handling\\\/\"},\"wordCount\":898,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/file-handling\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/File-Handling.webp\",\"articleSection\":[\"Python Course\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/file-handling\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/file-handling\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/file-handling\\\/\",\"name\":\"File Handling - Python Course\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/file-handling\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/file-handling\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/File-Handling.webp\",\"datePublished\":\"2025-05-19T11:38:34+00:00\",\"dateModified\":\"2025-05-20T12:56:49+00:00\",\"description\":\"File handling in Python involves opening, reading, writing, and managing files efficiently using built-in functions and context managers.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/file-handling\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/file-handling\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/file-handling\\\/#primaryimage\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/File-Handling.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/File-Handling.webp\",\"width\":1200,\"height\":628,\"caption\":\"File Handling\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/file-handling\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Courses\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Course\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/learn\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"File Handling\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"name\":\"BUHAVE\",\"description\":\"Courses - Learn Online for Free\",\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/buhave.com\\\/courses\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\",\"name\":\"BUHAVE\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"width\":375,\"height\":75,\"caption\":\"BUHAVE\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/BeYouHave\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/buhave\",\"https:\\\/\\\/www.youtube.com\\\/@buhave\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\",\"name\":\"Naveed Safdar\",\"description\":\"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/naveedsafdar\\\/\",\"https:\\\/\\\/www.facebook.com\\\/naveedsafdarawan\\\/\",\"https:\\\/\\\/www.youtube.com\\\/@naveedsafdar\"],\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/author\\\/naveed-safdar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"File Handling - Python Course","description":"File handling in Python involves opening, reading, writing, and managing files efficiently using built-in functions and context managers.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/buhave.com\/courses\/python\/file-handling\/","og_locale":"en_US","og_type":"article","og_title":"File Handling - Python Course","og_description":"File handling in Python involves opening, reading, writing, and managing files efficiently using built-in functions and context managers.","og_url":"https:\/\/buhave.com\/courses\/python\/file-handling\/","og_site_name":"BUHAVE","article_publisher":"https:\/\/www.facebook.com\/BeYouHave\/","article_author":"https:\/\/www.facebook.com\/naveedsafdarawan\/","article_published_time":"2025-05-19T11:38:34+00:00","article_modified_time":"2025-05-20T12:56:49+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/File-Handling.webp","type":"image\/webp"}],"author":"Naveed Safdar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Naveed Safdar","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/buhave.com\/courses\/python\/file-handling\/#article","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/python\/file-handling\/"},"author":{"name":"Naveed Safdar","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca"},"headline":"File Handling","datePublished":"2025-05-19T11:38:34+00:00","dateModified":"2025-05-20T12:56:49+00:00","mainEntityOfPage":{"@id":"https:\/\/buhave.com\/courses\/python\/file-handling\/"},"wordCount":898,"commentCount":0,"publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"image":{"@id":"https:\/\/buhave.com\/courses\/python\/file-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/File-Handling.webp","articleSection":["Python Course"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/buhave.com\/courses\/python\/file-handling\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/buhave.com\/courses\/python\/file-handling\/","url":"https:\/\/buhave.com\/courses\/python\/file-handling\/","name":"File Handling - Python Course","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/#website"},"primaryImageOfPage":{"@id":"https:\/\/buhave.com\/courses\/python\/file-handling\/#primaryimage"},"image":{"@id":"https:\/\/buhave.com\/courses\/python\/file-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/File-Handling.webp","datePublished":"2025-05-19T11:38:34+00:00","dateModified":"2025-05-20T12:56:49+00:00","description":"File handling in Python involves opening, reading, writing, and managing files efficiently using built-in functions and context managers.","breadcrumb":{"@id":"https:\/\/buhave.com\/courses\/python\/file-handling\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/buhave.com\/courses\/python\/file-handling\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/python\/file-handling\/#primaryimage","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/File-Handling.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/File-Handling.webp","width":1200,"height":628,"caption":"File Handling"},{"@type":"BreadcrumbList","@id":"https:\/\/buhave.com\/courses\/python\/file-handling\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Courses","item":"https:\/\/buhave.com\/courses\/"},{"@type":"ListItem","position":2,"name":"Python Course","item":"https:\/\/buhave.com\/courses\/learn\/python\/"},{"@type":"ListItem","position":3,"name":"File Handling"}]},{"@type":"WebSite","@id":"https:\/\/buhave.com\/courses\/#website","url":"https:\/\/buhave.com\/courses\/","name":"BUHAVE","description":"Courses - Learn Online for Free","publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/buhave.com\/courses\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/buhave.com\/courses\/#organization","name":"BUHAVE","url":"https:\/\/buhave.com\/courses\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","width":375,"height":75,"caption":"BUHAVE"},"image":{"@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/BeYouHave\/","https:\/\/www.linkedin.com\/company\/buhave","https:\/\/www.youtube.com\/@buhave"]},{"@type":"Person","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca","name":"Naveed Safdar","description":"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.","sameAs":["https:\/\/www.linkedin.com\/in\/naveedsafdar\/","https:\/\/www.facebook.com\/naveedsafdarawan\/","https:\/\/www.youtube.com\/@naveedsafdar"],"url":"https:\/\/buhave.com\/courses\/author\/naveed-safdar\/"}]}},"_links":{"self":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/comments?post=482"}],"version-history":[{"count":1,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/482\/revisions"}],"predecessor-version":[{"id":484,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/482\/revisions\/484"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media\/483"}],"wp:attachment":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media?parent=482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/categories?post=482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/tags?post=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}