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

file handling notes (Python dilip sir)

file handling in python notes

Uploaded by

Dilip Kumar
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)
5 views

file handling notes (Python dilip sir)

file handling in python notes

Uploaded by

Dilip Kumar
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/ 3

PYTHON FILE HANDLING Dilip Sir

(9431180168)
1. #Program to write product names in a text file >>> %Run prodtxtfile_write.py
using write() method. Enter name of product: soap
Enter name of product: pasta
f = open("c:\DilipSir\product.txt","w") Enter name of product: rice
if not f: Enter name of product: wheatEnter
print("File does not exist ") name of product: sugar
else: product.txt file view
for i in range(5):
n = input("Enter name of product: ")
f.write(n+"\n") #to store product name
in file in different lines
f.close()

Enter no. of records 3


2. #Program to write product names in a text file >>> %Run prodtxtfile_writelines.py

Enter name of product: mouse


using writelines() method.

Enter name of product: pen


n = int(input("Enter no. of records "))

Enter name of product: box


f = open("c:\DilipSir\product.txt","w")
if not f:
print("File does not exist ")
else: product.txt file view
L = [] #empty list
for i in range(n):
pname = input("Enter name of product: ")
L.append(pname+"\n")
f.writelines(L) #to store product names in
file at a time
f.close()

3. #Program to write the following poem in >>> %Run poemtxtfile_writelines.py


the file “poem.txt” in given format. **Where the mind is without fear and the
Where The Mind Is Without Fear - Poem By head is held high
Rabindranath Tagore **Where knowledge is free
Where the mind is without fear and the head is held **Where the world has not been broken up
high into fragments
Where knowledge is free **By narrow domestic walls
Where the world has not been broken up into **Where words come out from the depth of
fragments truth
By narrow domestic walls **Where tireless striving stretches its arms
Where words come out from the depth of truth towards perfection
Where tireless striving stretches its arms towards **Where the clear stream of reason has not
perfection lost its way
Where the clear stream of reason has not lost its **Into the dreary desert sand of dead habit
A. way **Where the mind is lead forward by thee
Into the dreary desert sand of dead habit **Into ever-widening thought and action
Where the mind is lead forward by thee **Into that heaven of freedom, my Father,
Into ever-widening thought and action let my country awake.
Into that heaven of freedom, my Father, let my
country awake.
f = open("c:\DilipSir\poem.txt","w")
if not f:
print("File does not exist ")
PYTHON FILE HANDLING Dilip Sir
(9431180168)
else:
L = [] #empty list
s="Where The Mind Is Without Fear -
Poem By Rabindranath Tagore"
for i in range (11):
s=input("**")
L.append(s+"\n")
f.writelines(L)
f.close()

Poem.txt file view

>>> %Run
poemtxtfile_countlines.py
4. #Write a function to count the number of lines

No. of lines not starting with


from a text file "poem.txt" which are not starting

'W' = 4
with an alphabet "W". Consider the file "poem.txt"
contains the following lines.
def line_count():
f = open("c:\DilipSir\poem.txt", "r")
count = 0
for line in f:
if line[0] not in 'W':
count = count+1
f.close()
print("No. of lines not starting with 'W' = ",count)

>>> %Run
line_count()

poemtxtfile_countwords.py
5. #Write a function in python to count and display

Total words are: 101


the total number of words in a text file.
def count_words():
f = open("c:\DilipSir\poem.txt","r")
count = 0
data = f.read() #reads the whole file
words = data.split() #list of words is formed
for w in words:
count = count +1
print("Total words are: ",count)
f.close()

>>> %Run
count_words()

poem_displaywords.py
6. #Write a function display_words() to read lines

Is - By is is is up By of of of is by
from a text file “poem.txt”, and display those

of my my
words which are less than 3 characters.
def display_words():
f = open("c:\DilipSir\poem.txt","r")
data = f.read() #reads the whole file
words = data.split() #list of words is formed
for w in words:
if len(w) < 3:
print(w,end = " ")
f.close()
PYTHON FILE HANDLING Dilip Sir
(9431180168)
>>> %Run
display_words()

poemtxtfile_count1words.py
7. #Write a function to count the words “Where” and
Total words are: 16
“the” present in a text file “poem.txt”.[Note that
the words “Where” and “the” are complete words]
def count_words():
f = open("c:\DilipSir\poem.txt","r")
count = 0
data = f.read()
words = data.split()
for w in words:
if w == "Where" or w == "the":
count = count +1
print("Total words are: ",count)
f.close()
count_words()
8. #Consider the following lines for the file Output
“sample.txt” and predict the output of the python

I am learning Python Language.


code given below. It has very simple

It has very simple syntax and easy to code.


syntax an

It is dynamically typed language.


d easy to code.

f = open("c:\DilipSir\sample.txt","r")
L = f.readline()
L2 = f.readline(18)
ch3 = f.read(10)
print(L2)
print(ch3)
print(f.readline())
f.close()

9. #Write a program to read a file “mytext.txt” which has Output


the following text. Use the seek() and tell() functions for 8

‘A penny saved is a penny earned’


performing the given task. saved is a penny earned

f = open("mytext.txt","r")
f.seek(8)
print(f.tell())
print(f.readline())
f.close()

You might also like