FILE HANDLING
A R U N A R U N I S T O
In python built-in functions input() and
print() is used for basic Input/Output
operations.
#Example
name = input("Enter your name: ")
print("My name is:",name)
#Output
"""
Enter your name: Arunisto
My name is: Arunisto
"""
In python there is an another method also
these types basic standard operations by
using sys module..
#using sys module
import sys
#for input
name = [Link]()
#for print
[Link](name)
Any object that interacts with input and
output stream is called file object. open()
function returns a file object.
The open() function creates a file object
which would be utilized to call another
support methods associated with it.
Syntax:
file_object = open(<file_name>,
<access_mode>,
<buffering>)
#sample
sample = open("[Link]", "w+")
print("File name:", [Link])
print("Txt file closed or not:", [Link])
print("Mode of the file:",[Link])
#result
"""
File name: [Link]
Txt file closed or not: False
Mode of the file: w+
"""
Modes and description
r - Open a file for reading only
rb - Open a file for reading only in binary
format
r+ - Opens a file for both reading and writing
rb+ - Opens a file for both in binary format
w - Opens a file for writing only
b - Opens the file in binary mode.
t - Opens the file in text mode (default)
+ - open file for updating(read & write)
wb - opens a file for writing only in binary
format
w+ - opens a file for both writing and reading
wb+ - opens file for both in binary
a - if we use 'w' it will overwrite the existing.
This is where we use 'a' it will opens a file for
appending
ab - opens a file for appending in binary
format
a+ - opens a file for both appending and
reading
ab+ - opens a file for both appending and
reading in binary format
x - open for exclusive creation
#opens a file for writing
details = open("[Link]", "w")
[Link]("Name: Arun Arunisto")
[Link]()
The code will create a file named “[Link]”
and it will write “Name: Arun Arunisto” on
that file.
#opens a file for writing in binary
details = open("[Link]", "wb")
data = b"Name: Arun Arunisto"
[Link](data)
[Link]()
Conversion of a string to bytes is also
possible using encode() function.
#opens a file for writing in binary
details = open("[Link]", "wb")
data = "Name: Arun Arunisto".encode('utf-8')
[Link](data)
[Link]()
#opens a file for appending
details = open("[Link]", "a")
[Link]("Name: Arun Arunisto")
[Link]()
Using w+ mode it's not possible to perform
write operation at any earlier byte position
in the file. The 'w+' mode enables using
write() as well as read() methods without
closing a file. The file object supports seek()
function to rewind the stream to any desired
byte position.
#opens a file for read and write mode
details = open("[Link]", "w+")
[Link]("Name: Arunisto")
[Link](6, 0)
data = [Link](8) #Arunisto
[Link](6, 0)
[Link]('Arun Arunisto')
[Link]()
#The ouput will be
"""
Name: Arun Arunisto
"""
#writing integer data in a binary file
num = 1915
data = num.to_bytes(8, 'big')
file = open('[Link]', 'wb')
[Link](data)
[Link]()
#reading from binary file
file = open('[Link]', 'rb')
data = [Link]()
print(data) #before
bin_to_int = int.from_bytes(data, 'big')
print(bin_to_int) #after
[Link]()
#output
"""
b'\x00\x00\x00\x00\x00\x00\x07{'
1915
"""
In python os module plays an important role
when the time comes to file-handling or
processing operations like renaming,
deleting etc
#renaming using os module
import os
"""
syntax:
[Link](<current_file_name>,
<new_file_name>)
"""
#example
[Link]("[Link]", "person_info.txt")
#removing a file using os module
import os
"""
syntax:
[Link](<file_name>)
"""
[Link]('[Link]')
And you can do lots of things using os
module and also using the file_object
Official documentation of os module:
[Link]
Official documentation of open() built-in
function:
[Link]
html#open