Course
File Handling
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
File Handling
When we use any computer application, some data needs to be provided. Data is stored in computer's main memory (RAM) until the application is running. Thereafter, memory contents from RAM are erased.
We would like to store it in such a way that it can be retrieved whenever required in a persistent medium such as a disk file.
Python uses built-in
input()
and print()
functions to perform standard input/output operations. Python program interacts with these IO devices through standard stream objects stdin and stdout defined in sys module.
The
input()
function reads bytes from a standard input stream device i.e. keyboard. Hence both the following statements read input from the user.name = input()#is equivalent toimport sysname = sys.stdin.readline()
The
print()
function on the other hand, sends the data towards standard output stream device, i.e., the display monitor. It is a convenience function emulating write()
method of stdout object.print (name)
#is equivalent toimport syssys.stdout.write(name)
Any object that interacts with input and output steam is called File object. Python's built-in function
open()
returns a file object.The open() Function
This function creates a file object, which would be utilized to call other support methods associated with it.
Syntax
file object = open(file_name [, access_mode][, buffering])
Here are the parameter details −
file_name − The file_name argument is a string value that contains the name of the file that you want to access.
access_mode − The access_mode determines the mode in which the file has to be opened, i.e., read, write, append, etc. A complete list of possible values is given below in the table. This is an optional parameter and the default file access mode is read (r).
buffering − If the buffering value is set to 0, no buffering takes place. If the buffering value is 1, line buffering is performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action is performed with the indicated buffer size. If negative, the buffer size is the system default (default behavior).
File Opening Modes
Following are the file opening modes
Once a file is opened and you have one file object, you can get various information related to that file.
Example
# Open a filefo = open("foo.txt", "wb")print ("Name of the file: ", fo.name)print ("Closed or not: ", fo.closed)print ("Opening mode: ", fo.mode)fo.close()
It will produce the following output
Name of the file: foo.txtClosed or not: FalseOpening mode: wb
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.