0% found this document useful (0 votes)
10 views

Python File Handling

The document discusses file handling in Python. It covers built-in functions like input(), print(), and open() to perform basic file operations. It also discusses the os module and how to rename, delete, read, and write to files in various modes like binary. Modes like r, w, a, rb, wb are explained for reading, writing, appending text and binary files.

Uploaded by

abduldiawade777
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Python File Handling

The document discusses file handling in Python. It covers built-in functions like input(), print(), and open() to perform basic file operations. It also discusses the os module and how to rename, delete, read, and write to files in various modes like binary. Modes like r, w, a, rb, wb are explained for reading, writing, appending text and binary files.

Uploaded by

abduldiawade777
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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 = sys.stdin.readline()

#for print
sys.stdout.write(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("sample.txt", "w+")
print("File name:", sample.name)
print("Txt file closed or not:", sample.closed)
print("Mode of the file:",sample.mode)

#result
"""
File name: sample.txt
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("info.txt", "w")
details.write("Name: Arun Arunisto")
details.close()
The code will create a file named “info.txt”
and it will write “Name: Arun Arunisto” on
that file.
#opens a file for writing in binary
details = open("info.txt", "wb")
data = b"Name: Arun Arunisto"
details.write(data)
details.close()
Conversion of a string to bytes is also
possible using encode() function.
#opens a file for writing in binary
details = open("info.txt", "wb")
data = "Name: Arun Arunisto".encode('utf-8')
details.write(data)
details.close()
#opens a file for appending
details = open("info.txt", "a")
details.write("Name: Arun Arunisto")
details.close()
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("info.txt", "w+")
details.write("Name: Arunisto")
details.seek(6, 0)
data = details.read(8) #Arunisto
details.seek(6, 0)
details.write('Arun Arunisto')
details.close()

#The ouput will be


"""
Name: Arun Arunisto
"""
#writing integer data in a binary file
num = 1915
data = num.to_bytes(8, 'big')
file = open('test.bin', 'wb')
file.write(data)
file.close()

#reading from binary file


file = open('test.bin', 'rb')
data = file.read()
print(data) #before
bin_to_int = int.from_bytes(data, 'big')
print(bin_to_int) #after
file.close()

#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:
os.rename(<current_file_name>,
<new_file_name>)
"""

#example
os.rename("info.txt", "person_info.txt")

#removing a file using os module


import os
"""
syntax:
os.remove(<file_name>)
"""
os.remove('info.txt')
And you can do lots of things using os
module and also using the file_object

Official documentation of os module:


https://docs.python.org/3/library/os.html

Official documentation of open() built-in


function:
https://docs.python.org/3/library/functions.
html#open

You might also like