{"id":31705,"date":"2024-11-01T09:01:56","date_gmt":"2024-11-01T09:01:56","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=31705"},"modified":"2024-11-01T11:48:35","modified_gmt":"2024-11-01T11:48:35","slug":"input-and-output-in-python-file-reading-and-writing","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/31705\/","title":{"rendered":"Input and Output in Python: File Reading and Writing"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>File input and output are essential elements in most programming languages, used for permanently storing data or importing external data into a program. Python provides powerful and intuitive features for easily performing these file input and output operations. In this tutorial, we will explain in detail how to read and write files using Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Opening and Closing Files<\/h2>\n\n\n\n<p>The first thing to do when working with files is to open the file in Python. To open a file, we use the built-in function <code>open()<\/code>. The <code>open()<\/code> function takes the file name and the file opening mode as parameters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file_object = open(\"example.txt\", \"r\") # Open file in read mode<\/code><\/pre>\n\n\n\n<p>After using the file, it must be closed. This releases system resources and prevents data corruption. Closing a file can be done using the <code>close()<\/code> method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file_object.close()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">File Opening Modes<\/h3>\n\n\n\n<p>When opening a file, various modes can be used depending on the file&#8217;s purpose. The main file opening modes are as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>'r'<\/code>: Read mode. Used to read the contents of the file. The file must exist.<\/li>\n\n\n\n<li><code>'w'<\/code>: Write mode. Used to write content to the file. If the file does not exist, a new file is created, and if the existing file exists, all contents are deleted.<\/li>\n\n\n\n<li><code>'a'<\/code>: Append mode. Used to append content to the end of the file. If the file does not exist, a new file is created.<\/li>\n\n\n\n<li><code>'b'<\/code>: Binary mode. Used for handling files in binary. For example, &#8216;rb&#8217; is binary read mode.<\/li>\n\n\n\n<li><code>'t'<\/code>: Text mode. The default, processes as a text file.<\/li>\n\n\n\n<li><code>'x'<\/code>: Exclusive creation mode. Creates a new file only if it does not exist. If the file already exists, an error occurs.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Reading Files<\/h2>\n\n\n\n<p>The process of reading files is mainly done through the methods <code>read()<\/code>, <code>readline()<\/code>, and <code>readlines()<\/code>. Depending on the file size and format, you can choose the appropriate method to use.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The read() Method<\/h3>\n\n\n\n<p>The <code>read()<\/code> method reads the contents of the file as a string. It can read the entire file content at once, or specify the number of bytes to read, and after the read operation, the file pointer will be positioned at the end of the file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"example.txt\", \"r\") as file:\n    content = file.read()\nprint(content)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">The readline() Method<\/h3>\n\n\n\n<p>The <code>readline()<\/code> method is useful for reading one line at a time. The newline character at the end of the line is included, and after the read operation, the file pointer moves to the next line.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"example.txt\", \"r\") as file:\n    line = file.readline()\n    while line:\n        print(line, end='')\n        line = file.readline()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">The readlines() Method<\/h3>\n\n\n\n<p>The <code>readlines()<\/code> method reads all lines from the file and returns them as a list. Each line is treated as an individual element in the list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"example.txt\", \"r\") as file:\n    lines = file.readlines()\nfor line in lines:\n    print(line, end='')<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Writing Files<\/h2>\n\n\n\n<p>The process of writing content to a file is done through the methods <code>write()<\/code> and <code>writelines()<\/code>. These methods provide various ways to write data to the file, allowing for flexibility in use.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The write() Method<\/h3>\n\n\n\n<p>The <code>write()<\/code> method writes string data to a file. After writing data, you must call <code>flush()<\/code> or close the file to ensure the data is actually written.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"example.txt\", \"w\") as file:\n    file.write(\"Hello, World!\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">The writelines() Method<\/h3>\n\n\n\n<p>The <code>writelines()<\/code> method takes a list of strings and writes them to the file. Each string is appended to the end of the existing line, so you may need to explicitly add newline characters at the end of each string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"example.txt\", \"w\") as file:\n    lines = [\"Line 1\\n\", \"Line 2\\n\", \"Line 3\\n\"]\n    file.writelines(lines)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Exception Handling for File Processing<\/h2>\n\n\n\n<p>File operations can encounter various errors, so it is important to manage exceptions effectively. You can manage exceptions using Python&#8217;s <code>try<\/code>&#8230;<code>except<\/code> statement, which allows you to handle exceptions such as file not found, read errors, and more.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    with open(\"example.txt\", \"r\") as file:\n        content = file.read()\n        print(content)\nexcept FileNotFoundError:\n    print(\"The file does not exist.\")\nexcept Exception as e:\n    print(f\"An exception occurred: {e}\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we covered the basic ways to read and write files in Python, the main methods, and how to manage exceptions during file processing. File input and output are essential functions for exchanging and storing application data with the outside world. By utilizing various file input and output methods and proper exception handling, you can write more robust Python programs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction File input and output are essential elements in most programming languages, used for permanently storing data or importing external data into a program. Python provides powerful and intuitive features for easily performing these file input and output operations. In this tutorial, we will explain in detail how to read and write files using Python. &hellip; <a href=\"https:\/\/atmokpo.com\/w\/31705\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Input and Output in Python: File Reading and Writing&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[98],"tags":[95],"class_list":["post-31705","post","type-post","status-publish","format-standard","hentry","category--en","tag--en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Input and Output in Python: File Reading and Writing - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/31705\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Input and Output in Python: File Reading and Writing - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Introduction File input and output are essential elements in most programming languages, used for permanently storing data or importing external data into a program. Python provides powerful and intuitive features for easily performing these file input and output operations. In this tutorial, we will explain in detail how to read and write files using Python. &hellip; \ub354 \ubcf4\uae30 &quot;Input and Output in Python: File Reading and Writing&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/31705\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:01:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:48:35+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/31705\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31705\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Input and Output in Python: File Reading and Writing\",\"datePublished\":\"2024-11-01T09:01:56+00:00\",\"dateModified\":\"2024-11-01T11:48:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31705\/\"},\"wordCount\":619,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"keywords\":[\"\ud30c\uc774\uc36c\uac15\uc88c\"],\"articleSection\":[\"Python Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/31705\/\",\"url\":\"https:\/\/atmokpo.com\/w\/31705\/\",\"name\":\"Input and Output in Python: File Reading and Writing - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:01:56+00:00\",\"dateModified\":\"2024-11-01T11:48:35+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31705\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/31705\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/31705\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Input and Output in Python: File Reading and Writing\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Input and Output in Python: File Reading and Writing - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","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:\/\/atmokpo.com\/w\/31705\/","og_locale":"ko_KR","og_type":"article","og_title":"Input and Output in Python: File Reading and Writing - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Introduction File input and output are essential elements in most programming languages, used for permanently storing data or importing external data into a program. Python provides powerful and intuitive features for easily performing these file input and output operations. In this tutorial, we will explain in detail how to read and write files using Python. &hellip; \ub354 \ubcf4\uae30 \"Input and Output in Python: File Reading and Writing\"","og_url":"https:\/\/atmokpo.com\/w\/31705\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:01:56+00:00","article_modified_time":"2024-11-01T11:48:35+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/31705\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/31705\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Input and Output in Python: File Reading and Writing","datePublished":"2024-11-01T09:01:56+00:00","dateModified":"2024-11-01T11:48:35+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/31705\/"},"wordCount":619,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"keywords":["\ud30c\uc774\uc36c\uac15\uc88c"],"articleSection":["Python Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/31705\/","url":"https:\/\/atmokpo.com\/w\/31705\/","name":"Input and Output in Python: File Reading and Writing - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:01:56+00:00","dateModified":"2024-11-01T11:48:35+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/31705\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/31705\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/31705\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Input and Output in Python: File Reading and Writing"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=31705"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31705\/revisions"}],"predecessor-version":[{"id":31706,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31705\/revisions\/31706"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=31705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=31705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=31705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}