{"id":9865,"date":"2021-07-03T11:43:40","date_gmt":"2021-07-03T06:13:40","guid":{"rendered":"https:\/\/pynative.com\/?p=9865"},"modified":"2021-07-03T11:55:52","modified_gmt":"2021-07-03T06:25:52","slug":"python-file-objects","status":"publish","type":"post","link":"https:\/\/pynative.com\/python-file-objects\/","title":{"rendered":"File Objects in Python"},"content":{"rendered":"\n<p>In this tutorial, you&#8217;ll learn file objects. Also, we will see how to use file object <strong>methods<\/strong> and <strong>attributes<\/strong> to perform various file operations.<\/p>\n\n\n\n<div class=\"wp-block-columns seriesbox is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong class=\"series\">Series<\/strong>: <a href=\"https:\/\/pynative.com\/python\/file-handling\/\">Python File Handling<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-yoast-seo-table-of-contents yoast-table-of-contents\"><h2>Table of contents<\/h2><ul><li><a href=\"#h-what-is-the-file-object\" data-level=\"2\">What is the File Object?<\/a><\/li><li><a href=\"#h-types-of-file-object\" data-level=\"2\">Types of File Object<\/a><ul><li><a href=\"#h-text-files-textiowrapper\" data-level=\"3\">Text files (TextIOWrapper)<\/a><\/li><li><a href=\"#h-binary-files-bufferedreader-and-bufferedwriter\" data-level=\"3\">Binary Files (BufferedReader and BufferedWriter)<\/a><\/li><li><a href=\"#h-raw-files\" data-level=\"3\">Raw Files<\/a><\/li><\/ul><\/li><li><a href=\"#h-file-object-attributes\" data-level=\"2\">File Object Attributes<\/a><\/li><li><a href=\"#h-file-object-methods\" data-level=\"2\">File Object Methods<\/a><ul><li><a href=\"#h-read-method\" data-level=\"3\">read() Method<\/a><\/li><li><a href=\"#h-readline-method\" data-level=\"3\">readline() Method<\/a><\/li><li><a href=\"#h-readlines-method\" data-level=\"3\">readlines() Method<\/a><\/li><li><a href=\"#h-readable-method\" data-level=\"3\">readable() Method<\/a><\/li><li><a href=\"#h-truncate-size-method\" data-level=\"3\">truncate(size) Method<\/a><\/li><li><a href=\"#h-write-method\" data-level=\"3\">write() Method<\/a><\/li><li><a href=\"#h-writelines-method\" data-level=\"3\">writelines() Method<\/a><\/li><li><a href=\"#h-writable-method\" data-level=\"3\">writable() Method<\/a><\/li><li><a href=\"#h-close-method\" data-level=\"3\">close() Method<\/a><\/li><li><a href=\"#h-seek-and-tell-method\" data-level=\"3\">seek() and tell() method<\/a><\/li><li><a href=\"#h-fileno-method\" data-level=\"3\">fileno() Method<\/a><\/li><li><a href=\"#h-flush-method\" data-level=\"3\">flush() Method<\/a><\/li><li><a href=\"#h-isatty-method\" data-level=\"3\">isatty() Method<\/a><\/li><\/ul><\/li><\/ul><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-the-file-object\">What is the File Object?<\/h2>\n\n\n\n<p>Python <strong>file object provides methods and attributes to access and manipulate files<\/strong>. Using file objects, we can read or write any files.<\/p>\n\n\n\n<p>Whenever we <a href=\"https:\/\/pynative.com\/python-file-open\/\">open a file<\/a> to perform any operations on it, Python returns a file object. To create a file object in Python use the built-in functions, such as <code>open()<\/code> and <code>os.popen()<\/code>.<\/p>\n\n\n\n<p>IOError exception is raised when a file object is misused, or file operation fails for an I\/O-related reason. For example, when you try to write to a file when a file is opened in read-only mode.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-types-of-file-object\">Types of File Object<\/h2>\n\n\n\n<p>In Python, there are three different categories of a file object, which are listed below:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Text files<\/li><li>Binary files<\/li><li>Raw files<\/li><\/ol>\n\n\n\n<p>All file types objects are defined in the <strong>io module<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-text-files-textiowrapper\">Text files (<code>TextIOWrapper<\/code>)<\/h3>\n\n\n\n<p>The text file type is most common. Usually, we use text files to store character data or storing information in plain text with no special formatting beyond basic fonts and font styles.<\/p>\n\n\n\n<p>We open a text file using the <code>open()<\/code> function. For example, <code>open('test'.txt', 'r')<\/code>. When we open a text file, it returns a <code>TextIOWrapper<\/code> file object.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code1\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">file = open(<span class=\"hljs-string\">'test.txt'<\/span>, <span class=\"hljs-string\">'w'<\/span>)\nprint(type(file))\n<span class=\"hljs-comment\"># Output: &lt;class '_io.TextIOWrapper'&gt; <\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code1', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-binary-files-bufferedreader-and-bufferedwriter\">Binary Files (<code>BufferedReader<\/code> and <code>BufferedWriter<\/code>)<\/h3>\n\n\n\n<p>Data is stored on a disk in the form of binary. For example, we use binary files to store data like images or videos. Binary files are a computer-readable form of storing data.<\/p>\n\n\n\n<p>A program is needed to interpret the data in a binary file and display it to the user. The binary files are also called buffered files. This file type is used for reading and writing binary data.<\/p>\n\n\n\n<p>Open the binary files using the open() function in binary mode. For example, <code>open('abc.txt', 'rb')<\/code>. It opens the file to read-only in binary mode. The file pointer exists at the beginning of the file.<\/p>\n\n\n\n<p>The <code>open()<\/code> function will return the <code>BufferedReader<\/code> when we open the binary file for reading and the <code>BufferedWriter<\/code> file object when we open a binary file for writing.<\/p>\n\n\n\n<p><strong>Example<\/strong> <\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code2\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">file = open(<span class=\"hljs-string\">'test.txt'<\/span>, <span class=\"hljs-string\">'rb'<\/span>)\nprint(type(file))\n<span class=\"hljs-comment\"># Output: &lt;class '_io.BufferedReader'&gt; <\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code2', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-raw-files\">Raw Files<\/h3>\n\n\n\n<p>A raw file is a collection of unprocessed data. It means the raw file has not been altered or manipulated in any way by the computer.<\/p>\n\n\n\n<p>The raw files are also called unbuffered files, and this file type is generally used as a low-level building block for binary and text streams. Mostly, The raw file is not used.<\/p>\n\n\n\n<p>When we open these files, using the <code>open()<\/code> function will return a <code>FileIO<\/code> object.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code3\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">file = open(<span class=\"hljs-string\">'test.txt'<\/span>, <span class=\"hljs-string\">'rb'<\/span>, buffering=<span class=\"hljs-number\">0<\/span>)\nprint(type(file))\n<span class=\"hljs-comment\"># Output: &lt;class '_io.FileIO'&gt; <\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code3', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-file-object-attributes\">File Object Attributes<\/h2>\n\n\n\n<p>File object has the following attributes that we can use to accessing various details of a file, such as a file name and under which mode the file is opened.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>name<\/code>: Return the name of the file. It is a read-only attribute and may not be present on all file-like objects. If the file object was created using the <code>open()<\/code> function, the file&#8217;s name is returned. Otherwise, some string indicates the source of the file object is returned.<\/li><li><code>encoding<\/code>: It returns the encoding this file uses, such as UTF-8. This attribute is read-only. When Unicode strings are written to a file, they will be converted to byte strings using this encoding. It may also be None. In that case, the file uses the system default encoding for converting Unicode strings.<\/li><li><code>mode<\/code>: Returns the file access mode used while opening a file.<\/li><li><code>closed<\/code>: Returns True if a file is closed. It is a boolean value indicating the current state of the file object.<\/li><li><code>newline<\/code>: Files opened in universal newline read mode keep track of the newlines encountered while reading the file. The values are &#8216;\\r&#8217;, &#8216;\\n&#8217;, &#8216;\\r\\n&#8217;, None (no newlines read yet), or a tuple containing all the newline types seen. For files not opened in universal newline read mode, the value of this attribute will be <code>None<\/code><\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code4\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">r'E:\\pynative\\files\\test.txt'<\/span>, <span class=\"hljs-string\">\"r\"<\/span>) <span class=\"hljs-keyword\">as<\/span> fp:\n    print(<span class=\"hljs-string\">'Is Closed:'<\/span>, fp.closed)\n    print(<span class=\"hljs-string\">'Encoding Used:'<\/span>, fp.encoding)\n    print(<span class=\"hljs-string\">'Access Mode:'<\/span>, fp.mode)\n    print(<span class=\"hljs-string\">'NewLines Found:'<\/span>, fp.newlines)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code4', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-file-object-methods\">File Object Methods<\/h2>\n\n\n\n<p>File object has the following methods that we can use to accessing a file: A file can be opened with a built-in function called <code>open()<\/code>. This function takes in the file\u2019s path and the access mode and returns a file object.<\/p>\n\n\n\n<p><strong>Read More<\/strong>: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/pynative.com\/python-file-open\/\">Open a file in Python<\/a><\/li><li><a href=\"https:\/\/pynative.com\/python-create-file\/\">Create a file in Python<\/a><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Method<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>read()<\/code><\/td><td>Returns the file content.<\/td><\/tr><tr><td><code>readable()<\/code><\/td><td>Returns whether the file stream can be read or not.<\/td><\/tr><tr><td><code>readline()<\/code><\/td><td>Read single line<\/td><\/tr><tr><td><code>readlines()<\/code><\/td><td>Read file into a list<\/td><\/tr><tr><td><code>truncate(size)<\/code><\/td><td>Resizes the file to a specified size.<\/td><\/tr><tr><td><code>writable()<\/code><\/td><td>Returns whether the file can be written to or not.<\/td><\/tr><tr><td><code>write()<\/code><\/td><td>Writes the specified string to the file.<\/td><\/tr><tr><td><code>writelines()<\/code><\/td><td>Writes a list of strings to the file.<\/td><\/tr><tr><td><code>close()<\/code><\/td><td>Closes the opened file.<\/td><\/tr><tr><td><code>seek()<\/code><\/td><td>Set file pointer position in a file<\/td><\/tr><tr><td><code>seekable()<\/code><\/td><td>Returns whether the file allows us to change the file position.<\/td><\/tr><tr><td><code>tell()<\/code><\/td><td>Returns the current file location.<\/td><\/tr><tr><td><code>detach()<\/code><\/td><td>Returns the separated raw stream from the buffer<\/td><\/tr><tr><td><code>fileno()<\/code><\/td><td>Returns a number that represents the stream, from the operating system&#8217;s perspective.<\/td><\/tr><tr><td><code>flush()<\/code><\/td><td>Flushes the internal buffer.<\/td><\/tr><tr><td><code>isatty()<\/code><\/td><td>Returns whether the file stream is interactive or not.<\/td><\/tr><\/tbody><\/table><figcaption>File Object Methods<\/figcaption><\/figure>\n\n\n\n<p>Let&#8217;s see each method one by one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-read-method\"><code>read()<\/code> Method<\/h3>\n\n\n\n<p><strong>Syntax<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">file_object.read(size)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\"><li>The <code>size<\/code> represents the <strong>number of bytes to read<\/strong> from a file. It returns file content in a string object.<\/li><li>If <code>size<\/code> is not specified, it <strong>reads all content from a file<\/strong><\/li><li>If the size argument is negative or not specified, read all data until EOF is reached.<\/li><li>An empty string is returned when EOF is encountered immediately.<\/li><li>When used in non-blocking mode, less data than requested may be returned, even if no size parameter was given.<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code5\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># read(size)<\/span>\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">r'E:\\pynative\\files\\test.txt'<\/span>, <span class=\"hljs-string\">'r'<\/span>) <span class=\"hljs-keyword\">as<\/span> fp:\n    <span class=\"hljs-comment\"># read 14 bytes<\/span>\n    <span class=\"hljs-comment\"># fp is file object<\/span>\n    print(fp.read(<span class=\"hljs-number\">14<\/span>))\n\n    <span class=\"hljs-comment\"># read all remaining content<\/span>\n    print(fp.read())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code5', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">My First Line\n\nMy Second Line\nMy Third Line<\/pre>\n\n\n\n<p><strong>Read More<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Complete Guide on <a href=\"https:\/\/pynative.com\/python-read-file\/\">Reading Files in Python<\/a><\/li><li><a href=\"https:\/\/pynative.com\/python-read-specific-lines-from-a-file\/\">Read Specific Lines From a File in Python<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-readline-method\"><code>readline()<\/code> Method<\/h3>\n\n\n\n<p><strong>Syntax<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">file_object.readline(size)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\"><li>Read one line from a file at a time. It returns the line in a string format.<\/li><li>If the <code>size<\/code> is given it reads the number of bytes (including the trailing newline) from a file.<\/li><li>If the size argument is negative or not specified, it read a single line<\/li><li>An empty string is returned when EOF is encountered immediately.<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code6\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># readline(size)<\/span>\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">r'E:\\pynative\\files\\test.txt'<\/span>, <span class=\"hljs-string\">'r'<\/span>) <span class=\"hljs-keyword\">as<\/span> fp:\n    <span class=\"hljs-comment\"># read single line<\/span>\n    print(fp.readline())\n\n    <span class=\"hljs-comment\"># read next line<\/span>\n    print(fp.readline())\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code6', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">My First Line\n\nMy Second Line<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-readlines-method\"><code>readlines()<\/code> Method<\/h3>\n\n\n\n<p><strong>Syntax<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">file_object.readlines(size)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\"><li>Read all lines from a file and return them in the form of a <a href=\"https:\/\/pynative.com\/python-lists\/\">list<\/a> object.<\/li><li>If the <code>sizehint<\/code> argument is present, instead of reading the entire file, whole lines totaling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code7\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># read(size)<\/span>\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">r'E:\\pynative\\files\\test.txt'<\/span>, <span class=\"hljs-string\">'r'<\/span>) <span class=\"hljs-keyword\">as<\/span> fp:\n    <span class=\"hljs-comment\"># read all lines<\/span>\n    print(fp.readlines())\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code7', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">['My First Line\\n', 'My Second Line\\n', 'My Third Line']<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-readable-method\">readable() Method<\/h3>\n\n\n\n<p>It checks whether the file stream can be read or not.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code8\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># read(size)<\/span>\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">r'E:\\pynative\\files\\test.txt'<\/span>, <span class=\"hljs-string\">'r'<\/span>) <span class=\"hljs-keyword\">as<\/span> fp:\n    <span class=\"hljs-comment\"># check if file object is readable<\/span>\n    print(fp.readable())\n<span class=\"hljs-comment\"># Output True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code8', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-truncate-size-method\"><code>truncate(size)<\/code> Method<\/h3>\n\n\n\n<p>Use the truncate() method to make the file empty.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If the optional size argument is present, the file is truncated to (at most) that size. So, for example, if you specify 10 bytes, truncate() will remove the first ten bytes from a file.<\/li><li>The size defaults to the current position of a file pointer<\/li><li>The current file position is not changed. Note that if a specified size exceeds the file&#8217;s current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content. Availability: Windows, many Unix variants.<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code9\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">r'E:\\pynative\\files\\test.txt'<\/span>, <span class=\"hljs-string\">'a'<\/span>) <span class=\"hljs-keyword\">as<\/span> fp:\n    fp.truncate(<span class=\"hljs-number\">5<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code9', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-write-method\"><code>write()<\/code> Method<\/h3>\n\n\n\n<p>Write a string to the file. If buffering is used, the line may not show up in the file until the flush() or close() method is called.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code10\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">r'E:\\pynative\\files\\test.txt'<\/span>, <span class=\"hljs-string\">'w'<\/span>) <span class=\"hljs-keyword\">as<\/span> fp:\n    fp.write(<span class=\"hljs-string\">'My New Line'<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code10', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<p><strong>Read More<\/strong>: Complete guide on <a href=\"https:\/\/pynative.com\/python-write-file\/\">write to file in Python<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-writelines-method\"><code>writelines()<\/code> Method<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Write a list of strings to the file. Use to write multiple lines at a time to a file. You can write any iterable object producing strings, typically a list of strings.<\/li><li>Note: <code>writelines()<\/code> do not add line separators.<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code11\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">r'E:\\pynative\\files\\test.txt'<\/span>, <span class=\"hljs-string\">'w'<\/span>) <span class=\"hljs-keyword\">as<\/span> fp:\n    data = &#91;<span class=\"hljs-string\">'line1\\n'<\/span>, <span class=\"hljs-string\">'line2\\n'<\/span>, <span class=\"hljs-string\">'line3\\n'<\/span>]\n    fp.writelines(data)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code11', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-writable-method\"><code>writable()<\/code> Method<\/h3>\n\n\n\n<p>It checks whether the file can be written to or not.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code12\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># read(size)<\/span>\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">r'E:\\pynative\\files\\test.txt'<\/span>, <span class=\"hljs-string\">'w'<\/span>) <span class=\"hljs-keyword\">as<\/span> fp:\n    <span class=\"hljs-comment\"># check if file object is readable<\/span>\n    print(fp.writeable())\n<span class=\"hljs-comment\"># Output True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code12', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-close-method\"><code>close()<\/code> Method<\/h3>\n\n\n\n<p>Close the opened file. A closed file cannot be read or written anymore. The <code>ValueError<\/code> will be raised if you try to read or write a closed file.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code13\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">fp = open(<span class=\"hljs-string\">r'E:\\pynative\\files\\test.txt'<\/span>, <span class=\"hljs-string\">'r'<\/span>)\nprint(fp.read())\n<span class=\"hljs-comment\"># close file<\/span>\nfp.close()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code13', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<p><strong>Note<\/strong>: It is good practice to open a file using the <code>with<\/code> statement. It automatically closes the file and ensures that all the resources tied up with the file are released.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-seek-and-tell-method\"><code>seek()<\/code> and <code>tell()<\/code> method<\/h3>\n\n\n\n<p>The&nbsp;<code>seek()<\/code>&nbsp;function&nbsp;<strong>sets the position of a file pointer<\/strong>&nbsp;and the&nbsp;<code>tell()<\/code>&nbsp;function&nbsp;<strong>returns the current position<\/strong>&nbsp;of a file pointer.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code14\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">r'E:\\pynative\\files\\test.txt'<\/span>, <span class=\"hljs-string\">\"r\"<\/span>) <span class=\"hljs-keyword\">as<\/span> fp:\n    <span class=\"hljs-comment\"># Moving the file handle to 6th character<\/span>\n    fp.seek(<span class=\"hljs-number\">6<\/span>)\n    <span class=\"hljs-comment\"># read file<\/span>\n    print(fp.read())\n\n    <span class=\"hljs-comment\"># get current position of file pointer<\/span>\n    print(fp.tell())\n\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code14', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<p><strong>Read More<\/strong>: Complete Guide <a href=\"https:\/\/pynative.com\/python-file-seek\/\">Python File Seek(): Move File Pointer Position<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-fileno-method\"><code>fileno()<\/code> Method<\/h3>\n\n\n\n<p>Return the integer file descriptor used by the underlying implementation system to request I\/O operations from the operating system. This can be useful for other lower-level interfaces that use file descriptors, such as <code>os.read()<\/code>.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code15\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">r'E:\\pynative\\files\\test.txt'<\/span>, <span class=\"hljs-string\">\"r\"<\/span>) <span class=\"hljs-keyword\">as<\/span> fp:\n    print(fp.fileno())\n<span class=\"hljs-comment\"># Output 3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code15', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-flush-method\"><code>flush()<\/code> Method<\/h3>\n\n\n\n<p>As the name suggests, it flushes the internal buffer. When buffering is used, and if you are writing to a file. the line may not show up in the file until the flush() or close() method is called.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code16\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">r'E:\\pynative\\files\\test.txt'<\/span>, <span class=\"hljs-string\">\"w\"<\/span>) <span class=\"hljs-keyword\">as<\/span> fp:\n    fp.write(<span class=\"hljs-string\">'New Line'<\/span>)\n    fp.flush()\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code16', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-isatty-method\"><code>isatty()<\/code> Method<\/h3>\n\n\n\n<p>Return True if the file is connected to a TTY device like a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Teleprinter\" target=\"_blank\" rel=\"noreferrer noopener\">teleprinter<\/a>, else False. It let you know that whether the file stream is interactive or not.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code17\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">r'E:\\pynative\\files\\test.txt'<\/span>, <span class=\"hljs-string\">\"r\"<\/span>) <span class=\"hljs-keyword\">as<\/span> fp:\n    print(fp.isatty())\n<span class=\"hljs-comment\"># Output False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code17', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn file objects. Also, we will see how to use file object methods and attributes to perform various file operations. Series: Python File Handling What is the File Object? Python file object provides methods and attributes to access and manipulate files. Using file objects, we can read or write any files. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[22,33],"tags":[34],"class_list":{"0":"post-9865","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python","7":"category-file-handling","8":"tag-file-handling","9":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python File Objects [Guide] &#8211; PYnative<\/title>\n<meta name=\"description\" content=\"Learn file objects in Python. Use file object methods and attributes to perform various file operations.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/pynative.com\/python-file-objects\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"File Objects in Python\" \/>\n<meta property=\"og:description\" content=\"Learn file objects in Python. Use file object methods and attributes to perform various file operations.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/pynative.com\/python-file-objects\/\" \/>\n<meta property=\"og:site_name\" content=\"PYnative\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-03T06:13:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-03T06:25:52+00:00\" \/>\n<meta name=\"author\" content=\"Vishal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@PyNative\" \/>\n<meta name=\"twitter:site\" content=\"@PyNative\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-file-objects\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-file-objects\\\/\"},\"author\":{\"name\":\"Vishal\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"headline\":\"File Objects in Python\",\"datePublished\":\"2021-07-03T06:13:40+00:00\",\"dateModified\":\"2021-07-03T06:25:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-file-objects\\\/\"},\"wordCount\":1508,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"keywords\":[\"File Handling\"],\"articleSection\":[\"Python\",\"Python File Handling\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-file-objects\\\/#respond\"]}],\"accessibilityFeature\":[\"tableOfContents\"]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-file-objects\\\/\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-file-objects\\\/\",\"name\":\"Python File Objects [Guide] &#8211; PYnative\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\"},\"datePublished\":\"2021-07-03T06:13:40+00:00\",\"dateModified\":\"2021-07-03T06:25:52+00:00\",\"description\":\"Learn file objects in Python. Use file object methods and attributes to perform various file operations.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-file-objects\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-file-objects\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-file-objects\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/pynative.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\\\/\\\/pynative.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"File Handling\",\"item\":\"https:\\\/\\\/pynative.com\\\/python\\\/file-handling\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"File Objects in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\",\"url\":\"https:\\\/\\\/pynative.com\\\/\",\"name\":\"PYnative\",\"description\":\"Python Programming\",\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/pynative.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\",\"name\":\"Vishal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"width\":968,\"height\":1065,\"caption\":\"Vishal\"},\"logo\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\"},\"description\":\"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!\",\"sameAs\":[\"https:\\\/\\\/pynative.com\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python File Objects [Guide] &#8211; PYnative","description":"Learn file objects in Python. Use file object methods and attributes to perform various file operations.","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:\/\/pynative.com\/python-file-objects\/","og_locale":"en_US","og_type":"article","og_title":"File Objects in Python","og_description":"Learn file objects in Python. Use file object methods and attributes to perform various file operations.","og_url":"https:\/\/pynative.com\/python-file-objects\/","og_site_name":"PYnative","article_published_time":"2021-07-03T06:13:40+00:00","article_modified_time":"2021-07-03T06:25:52+00:00","author":"Vishal","twitter_card":"summary_large_image","twitter_creator":"@PyNative","twitter_site":"@PyNative","twitter_misc":{"Written by":"Vishal","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/pynative.com\/python-file-objects\/#article","isPartOf":{"@id":"https:\/\/pynative.com\/python-file-objects\/"},"author":{"name":"Vishal","@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"headline":"File Objects in Python","datePublished":"2021-07-03T06:13:40+00:00","dateModified":"2021-07-03T06:25:52+00:00","mainEntityOfPage":{"@id":"https:\/\/pynative.com\/python-file-objects\/"},"wordCount":1508,"commentCount":1,"publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"keywords":["File Handling"],"articleSection":["Python","Python File Handling"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/pynative.com\/python-file-objects\/#respond"]}],"accessibilityFeature":["tableOfContents"]},{"@type":"WebPage","@id":"https:\/\/pynative.com\/python-file-objects\/","url":"https:\/\/pynative.com\/python-file-objects\/","name":"Python File Objects [Guide] &#8211; PYnative","isPartOf":{"@id":"https:\/\/pynative.com\/#website"},"datePublished":"2021-07-03T06:13:40+00:00","dateModified":"2021-07-03T06:25:52+00:00","description":"Learn file objects in Python. Use file object methods and attributes to perform various file operations.","breadcrumb":{"@id":"https:\/\/pynative.com\/python-file-objects\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/pynative.com\/python-file-objects\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/pynative.com\/python-file-objects\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/pynative.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/pynative.com\/python\/"},{"@type":"ListItem","position":3,"name":"File Handling","item":"https:\/\/pynative.com\/python\/file-handling\/"},{"@type":"ListItem","position":4,"name":"File Objects in Python"}]},{"@type":"WebSite","@id":"https:\/\/pynative.com\/#website","url":"https:\/\/pynative.com\/","name":"PYnative","description":"Python Programming","publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/pynative.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f","name":"Vishal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","url":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","width":968,"height":1065,"caption":"Vishal"},"logo":{"@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg"},"description":"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!","sameAs":["https:\/\/pynative.com"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"Vishal","author_link":"https:\/\/pynative.com\/author\/vishal\/"},"_links":{"self":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/9865","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/comments?post=9865"}],"version-history":[{"count":0,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/9865\/revisions"}],"wp:attachment":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/media?parent=9865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/categories?post=9865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/tags?post=9865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}