Fatskills
Practice. Master. Repeat.
Study Guide: Python Programming: I/O Input Used in Python
Source: https://www.fatskills.com/python/chapter/python-programming-i-o-input-used-in-python

Python Programming: I/O Input Used in Python

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~7 min read

We now know how o use print function for printing the output on screen or console. Here, we are going to learn about I/O (accepting input and printing output on console) and basic file operations.

Python print Function
By using simple print function as print (set of strings or variables), the system will print the content on to the console.
E.g., print (“Hello World!”)

Python input Function
Python has input ([prompt]) as its in-built function that read one line from standard input assuming it as a valid Python expression and returns it to the system as a string.

Below is an example.

Opening and Closing files in Python
Python language provides basic functions and methods which are necessary to manipulate files by default. We can do the file manipulation in Python by using a file object.
The open () Function

Python has in-built open () function that creates a file object with which we can read or write a file and call other support methods associated with it.

Syntax

file object = open(file_name [, access_mode][, Buffering])

 

Description of the parameter in detail:
- file_name:
It is a string value that contains the name of the file that we want to access.
- access_mode: It determines the mode in which the file has to be opened, i.e., write, read, append, etc. Given below is the complete list of possible values in the table. This parameter is optional with default file access mode as read (r).

 

 

Modes

Description

r

This mode opens a file in read only mode. Beginning of the file has the file pointer. This is the default mode.

rb

This mode opens a file in read only mode in binary format. Beginning of the file has the file pointer. This is the default mode.

r+

This mode opens a file in both reading and writing mode. Beginning of the file has the file pointer.

rb+

This mode opens a file in both reading and writing mode in binary format. Beginning of the file has the file pointer.

w

This mode opens a file in writing only mode. It overwrites the file if the file exists. If the file does not exist, then it creates a new file for writing.

wb

This ode opens a file in writing only mode in binary format. It overwrites the file if the file exists. If the file does not exist, then it creates a new file for writing.

w+

This mode opens a file in both writing and reading mode. It overwrites the existing file if the file exists. If the file does not exist, then it creates a new file for reading and writing.

wb+

This mode opens a file in both writing and reading mode in binary format. It overwrites the existing file if the file exists. If the file does not exist, then it creates a new file for reading and writing.

a

This mode opens a file for appending. The file pointer is present at the end of the file if the file exists. If the file does not exist, then it creates a new file for writing.

ab

This mode opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. If the file does not exist, then it creates a new file for writing.

a+

This mode opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. If the file does not exist, then it creates a new file for reading and writing.

ab+

This mode opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. If the file does not exist, then it creates a new file for reading and writing.



- Buffering: It can have value as negative, 0, 1, 2, etc. Depending on these values, if it is set to 0 then no buffering takes place. If it is set to 1 then line buffering is performed while accessing a file. If this buffering value as an integer greater than 1 then buffering action takes place with the indicated buffer size. If the value is negative, then the buffer size has the system default behavior.

The file Object Attributes
When a data file is opened then we get a file object using which we get various information related to that file.

Given below are the file attributes which provide the information associated with the file object.

 

 

Attribute

Description

file.closed

This attribute returns true if file is closed, otherwise false.

file.mode

This attribute returns access mode with which file was opened.

file.name

This attribute returns name of the file.

file.softspace

This attribute returns false if space explicitly required with print, otherwise true.



Below is the example for reading file Attributes from a file object.

Python in-built Methods for File Operations
Below table has the in-built function name and description for a file.

 

 

Methods

Description

close()

The close () method of a file object flushes any unwritten information and closes the file object. Once file is closed the no more writing can be done. E.g. fileObject.close ().

write()

The write () method writes string data to an open file. Python strings can have binary data as well as text. It is to be note that the write () method does not add a newline character ('\n') to the end of the string. E.g. fileObject.write (string).

read()

The read () method reads a string data from an open file. Python strings can have binary data as well as text data. E.g. fileObject.read ([count]); count is the passed parameter which represents the number of bytes to be read from the opened file. If count is missing, then it tries to read data from file as much as possible until the end of file.

tell()

The function tell () method tells us about the current position within the file. It tells us about position of the next read or write of string data at that many bytes from the beginning of the file. E.g. fileObject.tell ().

seek(offset[, from])

The function seek (offset [, from]) is used to change the current file position. The offset argument indicates the number of bytes that to be moved. The ‘from’ argument specifies the reference position from where the bytes are required to be moved.If from has value set to 0, it means use the beginning of the file as the reference position and it has value set to 1 means use the current position as the reference position and if it has value set to 2 then the end of the file would be taken as the reference position. E.g. fileObject.seek (0, 0).

rename()

The rename () method takes two arguments, the current filename and the new filename. E.g. fileObject.rename (current_file_name, new_file_name).

remove()

The remove () method is used to delete files by supplying the name. It can be used to delete files by supplying the name of the file to be deleted as the argument. E.g. fileObject.remove (current_file_name).

mkdir()

The mkdir () method of the os module is used to create directories in the current directory. We need to supply an argument to this method that contains the name of the directory to be created. E.g. os.mkdir ("newdir").

chdir()

The chdir () method is used to change the current directory. The chdir () method takes an argument, that is the name of the directory that you want to make the current directory. E.g. os.chdir ("newdir").

getcwd()

The getcwd () method is used to display the current working directory. E.g. os.getcwd ().

rmdir()

The rmdir () method is used to delete the directory, which is passed as an argument in the method. E.g. os.rmdir ('dirname').

 


Example on file operations using Python in-built file methods