0% found this document useful (0 votes)
29 views10 pages

File - Handling Part1

The document discusses file handling in Python. It describes different types of files like text, binary and CSV files. It covers concepts like opening, reading, writing and closing files as well as functions like read(), readline(), readlines() etc. It provides examples of counting characters, words, lines in a file and manipulating the file pointer. The document also briefly explains standard input, output and error streams in Python.

Uploaded by

uareidiot451
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)
29 views10 pages

File - Handling Part1

The document discusses file handling in Python. It describes different types of files like text, binary and CSV files. It covers concepts like opening, reading, writing and closing files as well as functions like read(), readline(), readlines() etc. It provides examples of counting characters, words, lines in a file and manipulating the file pointer. The document also briefly explains standard input, output and error streams in Python.

Uploaded by

uareidiot451
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/ 10

FILE HANDLING

 Introduction to files

Files or Data Files are the Files that store data pertaining to a specific application, for later use.
The data files can be stored in various ways.

 Types of files
o Text file
 A text files stores information in ASCII or Unicode characters.
 In text file every line is terminated or delimited with a special character called
EOL (End Of Line) character.
 In Python the default EOL character is a new line character i.e (\n).
o Binary file
 A Binary Fie is a file that contains information in machine format .
 There is no delimeter for a line.
 No translation occur as a result the processing is faster and easier for a
program to read and write as compared to text files
 Binary format is the best and secure way to store an application data.
o CSV file
 CSV stands for Comma Separated Values
 Every record field values are separated with comma by default
 It is a standard format of storing data
 It can be used for data exchange in various applications
 Relative and Absolute paths
o Path mentioned from from the top most level of hierarchy is known as Absolute
Path.
o Path mentioned relative to the current working directory is known as Relative Path.

Valid path declarations

"d:\\myfolder\\test\\STory.txt"

r"d:\myfolder\test\STory.txt"

"d:/myfolder/test/STory.txt"

"c://myfolder//test//story.txt"

Handling Text File


To operate with text file we use some Built in functions.

 open() is used to open a file


Syntax to open a File :

fobj =open("File name","File Mode")

Different File modes are :-

 r for read
 w for write
 a for append
 r+ for read and write
 w+ write and read
 a+ append and read

 read or write or append data

To read from a text file the functions are

o reading from a text file using


 read()
 read() ->reads all data of file in string format
 read(n) -> reads n numbers of characters from the file
 readline() -> reads one line at a time
 readlines() -> reads all the lines at a time in list format
o To write data into a text file
 write(str)
 writelines(L)

 close() is used to close a file

Syntax to close a File :

fobj.close()

 Some more functions


 seek()-> moves the file pointer from one position to another
position

fileobj.seek(offset,reference point)

where offset is the number of bytes to move .

Reference point tells the position from where to move the file pointer . It
can be 0 (from beginning) , 1 (current position), 2 (end position).

 tell()- >This function returns the current file position

pos=fileobj.tell()

 flush() -> This function forcefully writes the content from buffer to
file .

Example 1 : Command to open a file story.txt in writing mode.

f=open("Story.txt","w")

Example 2: Command to create a file Story.txt in d:\\myfolder\\test location and write content in it.

f=open("d:\\myfolder\\test\\STory.txt","w")
Examples:

#Write one line of content to a file named Work.txt

'''

f=open("work.txt","a")

f.write("We work hard with full zest. \n")

f.close()

'''

#Write a list of student names in student.txt using writelines()

'''

f=open("Student.txt","w")

list1=[]

for i in range(5):

name=input("enter student name")

list1.append(name+'\n')

f.writelines(list1)

f.close()

'''

#open file work.txt and read 10 characters from it.

'''

f=open("work.txt","r")

data=f.read(10)

print(data)

f.close()

'''

#open file work.txt and read entire content from it.

'''

f=open("work.txt","r")

data=f.read()

print(data)

f.close()

'''
#read from file work.txt

'''

f=open("work.txt","r")

data1=f.read(100)

data2=f.read(8)

print(data1)

print(data2)

f.close()

'''

#seek() and tell()

'''

f=open("work.txt","r")

pos=f.tell()

print("Current position is ",pos)

f.seek(10)

data=f.read(5)

pos=f.tell();

print("After read Current position is ",pos)

print("Data is ",data)

f.close();

'''

'''

f=open("work.txt","r+")

pos=f.tell()

print("Current position is ",pos)

f.seek(10)

f.write("ABCDEF")

f.close();

'''

#Count how many characters are present in a file

#or find the size of file work.txt


'''

f=open("work.txt","r")

data=f.read()

size=len(data)

print("Number of characters ",size)

f.close()

'''

#count how many 'w' are there in a file work.txt

'''

f=open("work.txt","r")

data=f.read()

cnt=0

for ch in data:

if ch=='w' or ch=='W':

cnt += 1

print("Number of w available ",cnt)

#print(data.count('w'))

f.close()

'''

# read 4 characters from 11 th position.

'''

f=open("work.txt","r")

f.seek(10)

data=f.read(4)

print(data)

'''

#seek() is used to move the file pointer from current position

#to a specified position

'''
f=open("work.txt","r")

f.seek(10)

data=f.read(50)

print(data)

'''

#count how many vowels are available in work.txt

'''

f=open("work.txt","r")

data=f.read()

data=data.lower()

cnt=0

for ch in data:

if ch in 'aeiou' :

cnt +=1

print("Number of vowels ",cnt)

f.close()

'''

#how many digits are there

#count how many uppercase letters / lowercase latters are there

'''

f=open("work.txt","r")

data=f.read()

cnt=0

for ch in data:

if ch.isdigit() :

cnt +=1

print("No of vowels ",cnt)

f.close()

'''
#count number of words in a text file

'''

f=open("work.txt","r")

data=f.read()

wordlist=data.split()

print(wordlist)

print("No of words ",len(wordlist))

f.close()

'''

#count how many words begin with w in text file work.txt

'''

f=open("work.txt","r")

data=f.read()

cnt=0

wordlist=data.split()

for word in wordlist:

if word[0]=='w' or word[0]=='W':

cnt+=1

print("Number of words begin with w are",cnt)

f.close()

'''

#count how many we or We are there the file work.txt

'''

f=open("work.txt","r")

data=f.read()

cnt=0

wordlist=data.split()

for word in wordlist:

if word=='we' or word=='We':

cnt+=1

print("Number of we present in file",cnt)


f.close()

'''

#readline function is used to read one line at a time

'''

f=open("work.txt")

data1=f.readline()

data2=f.readline()

print(data1,data2)

f.close()

'''

#read the 5th line

'''

f=open("work.txt")

for i in range(5):

data=f.readline()

print(data)

f.close()

'''

#readlines() reads all lines from the file in list format

'''

f=open("work.txt")

all_lines=f.readlines()

print(all_lines)

print(len(all_lines))

print(all_lines[4])

f.close()

'''
#count how many lines begin with W.

'''

f=open('work.txt')

all_lines=f.readlines()

cnt=0

for l in all_lines:

if l[0]=='W':

cnt +=1

print("Number of lines begin with W are",cnt)

f.close()

'''

#Count how many lines end with 'e' in file 'Work.txt'

'''

f=open('work.txt','r')

all_lines=f.readlines()

cnt=0

for l in all_lines:

if l[-1]=='e':

cnt +=1

print("Number of lines end with e are",cnt)

f.close()

'''

#remove all lines from 'work.txt' which starts with letter 'w'

f=open('work.txt')

all_lines=f.readlines()

cnt=0

for l in all_lines:

if l[0]=='W':

cnt +=1

print("Number of lines begin with W are",cnt)

f.close()
#Standard Input,Output,Error Streams

'''

stdin -> keyboard

stout-> monitor

stderr-> monitor

'''

import sys

line1=sys.stdin.readline()

print(line1)

‘’’’

import sys

line1=sys.stdin.readline()

sys.stdout.write(line1)

sys.stderr.write("No error occured")

You might also like