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

File Handling in Python

The document discusses file handling in Python, including opening and closing files, reading from and writing to files, and different file access modes. Common file methods like read(), write(), readline() are explained along with examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

File Handling in Python

The document discusses file handling in Python, including opening and closing files, reading from and writing to files, and different file access modes. Common file methods like read(), write(), readline() are explained along with examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

FILE HANDLING IN PYTHON

File Access Modes:


1) Read Only (‘r’)
2) Read and Write (‘r+’)
3) Write Only (‘w’)
4) Write and Read (‘w+’)
5) Append Only (‘a’)
6) Append and Read (‘a+’)

Opening a File:
Syntax:
File_object = open(r"File_Name","Access_Mode")

file1 = open("MyFile.txt","a")
file2 = open(r"D:\Text\MyFile2.txt","w+")
Closing a file:
Syntax:
File_object.close()
file1 = open("MyFile.txt","a")
file1.close()

Writing to a file:
1. write() :

File_object.write(str1)

2. writelines():

File_object.writelines(L) for L = [str1, str2, str3]


Reading from a file

1. read() :

File_object.read([n])

2. readline():

File_object.readline([n])

3. readlines():

File_object.readlines()

Exercise:
file1 = open("myfile.txt","w")
L = ["This is Delhi \n","This is Paris \n","This is London \n"]

file1.write("Hello \n")
file1.writelines(L)
file1.close()

file1 = open("myfile.txt","r+")

print "Output of Read function is "


print file1.read()
print

file1.seek(0)

print "Output of Readline function is "


print file1.readline()
print

file1.seek(0)

print "Output of Read(9) function is "


print file1.read(9)
print

file1.seek(0)
print "Output of Readline(9) function is "
print file1.readline(9)

file1.seek(0)

print "Output of Readlines function is "


print file1.readlines()
print
file1.close()

Output:

Output of Read function is


Hello
This is Delhi
This is Paris
This is London

Output of Readline function is


Hello

Output of Read(9) function is


Hello
Th

Output of Readline(9) function is


Hello

Output of Readlines function is


['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']
Appending to a file

file1 = open("myfile.txt","w")
L = ["This is Delhi \n","This is Paris \n","This is London \n"]
file1.close()

file1 = open("myfile.txt","a")#append mode


file1.write("Today \n")
file1.close()

file1 = open("myfile.txt","r")
print "Output of Readlines after appending"
print file1.readlines()
print
file1.close()

file1 = open("myfile.txt","w")#write mode


file1.write("Tomorrow \n")
file1.close()

file1 = open("myfile.txt","r")
print "Output of Readlines after writing"
print file1.readlines()
print
file1.close()

Output:

Output of Readlines after appending


['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing


['Tomorrow \n']
Files have the following methods:
 open(): Opens a file in given access mode.

open(file_address, access_mode)

 r
 r+
 w
 w+
 a
 a+

 read([size]):

f = open(__file__, 'r')

text = f.read(10)

print(text)

f.close()

 readline([size]):

f = open(__file__, 'r')
text = f.readline(20)
print(text)
f.close()

 readlines([sizehint]):

f = open(__file__, 'r')
text = f.readlines(25)
print(text)
f.close()

 write(string):

f = open(__file__, 'w')
line = 'Welcome Geeks\n'
f.write(line)
f.close()

More Examples in different modes:

Reading and Writing a file:

f = open(__file__, 'r+')
lines = f.read()
f.write(lines)
f.close()

Writing and Reading a file:

f = open(__file__, 'w+')
lines = f.read()
f.write(lines)
f.close()

Appending a file

f = open(__file__, 'a')
lines = 'Welcome Geeks\n'
f.write(lines)
f.close()

Appending and reading a file

f = open(__file__, 'a+')
lines = f.read()
f.write(lines)
f.close()

writelines(sequence):

Writing a file:

f = open(__file__, 'a+')
lines = f.readlines()
f.writelines(lines)
f.close()
 tell():

Telling the file object position:

f = open(__file__, 'r')
lines = f.read(10)
print(f.tell())
f.close()

 seek(offset, from_where):

Setting the file object position:

f = open(__file__, 'r')
lines = f.read(10)
print(lines)

print(f.seek(2,2))
lines = f.read(10)
print(lines)
f.close()

 flush():

# Clearing the internal buffer before closing the file


f = open(__file__, 'r')
lines = f.read(10)

#flush()
f.flush()
print(f.read())
f.close()

 fileno():

# Getting the integer file descriptor


f = open(__file__, 'r')
print(f.fileno())
f.close()
 isatty():

f = open(__file__, 'r')
print(f.isatty())
f.close()

 next():

# Iterates over the file


f = open(__file__, 'r')

try:
while f.next():
print(f.next())
except:
f.close()

 truncate([size]):

# Truncates the file


f = open(__file__, 'w')
f.truncate(10)
f.close()

 close():

# Opening and closing a file


f = open(__file__, 'r')

#close()
f.close()

Using write along with with() function

with open(“file.txt”, “w”) as f:


f.write(“Hello World!!!”)

split() using file handling

with open(“file.text”, “r”) as file:


data = file.readlines()
for line in data:
word = line.split()
print word

Attributes:
 closed
 encoding
 mode
 name
 newlines
 softspace

Exercise:

f = open(__file__, 'a+')
print(f.closed)
print(f.encoding)
print(f.mode)
print(f.newlines)
print(f.softspace)

You might also like