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

Text File Programs

The document contains code for performing various text file operations like creating a text file, displaying the contents of a text file, counting words or characters in a text file, and copying an existing text file. The code defines functions for file creation, display, word/character counting, and copying. It then calls the functions to demonstrate the file operations.

Uploaded by

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

Text File Programs

The document contains code for performing various text file operations like creating a text file, displaying the contents of a text file, counting words or characters in a text file, and copying an existing text file. The code defines functions for file creation, display, word/character counting, and copying. It then calls the functions to demonstrate the file operations.

Uploaded by

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

Text File Programs:

#
#
#
#
#

Program to :
1. Create Text File
2. Display the text file
3. Count Words given by the user
4. Exit

def fileCreation():
ofile = open("story.txt","w+")
choice = True
while True:
line = raw_input("enter sentence :")
ofile.write(line)
ofile.write('\n')
choice = raw_input("want to enter more data in file Y / N")
if choice.upper() == 'N' : break
ofile.close()
def display_text_file():
fo = open("story.txt", "r")
print "\n Displaying the text file contents using readlines() "
lines = fo.readlines()
fo.close()
for l in lines:
print l
def count_word():
fo = open("story.txt", "r")
print "\n Displaying each character of the the text file in separate lines
"
lines = fo.readlines()
word1=raw_input("Enter first word to be counted : ")
word2=raw_input("Enter second word to be counted : ")
fo.close()
count1=0
count2=0
for l in lines:
for w in l.split():
if w.upper()==word1.upper():
count1=count1+1
if w.upper()==word2.upper():
count2=count2+1
print word1," is repeated ",count1," times"
print word2," is repeated ",count2," times"
print "Creating the text file "
fileCreation()
print "Displaying the file "
display_text_file()
print "Counting the words in the file : "
count_word()

********************************************************************************
***********************
# Program to :

#
#
#
#

1. Create Text File


2. Display the text file
3. Count alphabets given by the user
4. Exit

def fileCreation():
ofile = open("story.txt","w+")
choice = True
while True:
line = raw_input("enter sentence :")
ofile.write(line)
ofile.write('\n')
choice = raw_input("want to enter more data in file Y / N")
if choice.upper() == 'N' : break
ofile.close()
def display_text_file():
fo = open("story.txt", "r")
print "\n Displaying the text file contents using readlines() "
lines = fo.readlines()
fo.close()
for l in lines:
print l
def count_alphabets():
fo = open("story.txt", "r")
print "\n Displaying each character of the the text file in separate lines
"
lines = fo.readlines()
alpha1=raw_input("Enter first alphabet to be counted : ")
alpha2=raw_input("Enter second alphabet to be counted : ")
fo.close()
count1=0
count2=0
for l in lines:
for w in l.split():
for c in w:
if c.upper()==alpha1.upper():
count1=count1+1
if c.upper()==alpha2.upper():
count2=count2+1
print alpha1," is repeated ",count1," times"
print alpha2," is repeated ",count2," times"
print "Creating the text file "
fileCreation()
print "Displaying the file "
display_text_file()
print "Counting the words in the file : "
count_alphabets()
"""
"""
********************************************************************************
*************************
# Program to count the number of uppercase and lowercase characters in the text
file
# 1. Create Text File

#
#
#
#

2. Display the text file


3. Read and display file Backwards
4. Display each character in separate line
5 Exit

def fileCreation():
ofile = open("story.txt","w+")
choice = True
while True:
line = raw_input("enter sentence :")
ofile.write(line)
ofile.write('\n')
choice = raw_input("want to enter more data in file Y / N")
if choice.upper() == 'N' : break
ofile.close()
def display_text_file():
fo = open("story.txt", "r")
print "\n Displaying the text file contents using readlines() "
lines = fo.readlines()
fo.close()
for l in lines:
print l
def display_each_char_in_separate_lines():
fo = open("story.txt", "r")
print "\n Displaying each character of the the text file in separate lines
"
lines = fo.readlines()
fo.close()
for l in lines:
for w in l.split():
for ch in w:
print ch
def read_display_file_backwards():
fo = open("story.txt", "r")
print "\n Displaying the text file contents backwards "
lines = fo.readlines()
print "Lines : ", lines
size=len(lines)
print "Number of lines =",size
fo.close()
for i in range(size-1,-1,-1):
print lines[i][::-1]

print "Creating the text file "


fileCreation()
print "Displaying the file "
display_text_file()
read_display_file_backwards()
display_each_char_in_separate_lines()
""""
""""

********************************************************************************
****************************
# Program to count the number of uppercase and lowercase characters in the text
file
# 1. Create Text File
# 2. Display the text file and
# 3. count the number of uppercase and lowercase characters in the text file
# 4. Exit
def fileCreation():
ofile = open("story.txt","w+")
choice = True
while True:
line = raw_input("enter sentence :")
ofile.write(line)
ofile.write('\n')
choice = raw_input("want to enter more data in file Y / N")
if choice.upper() == 'N' : break
ofile.close()
def display_count_upper_lower_case():
fo = open("story.txt", "r")
print "\n Displaying the text file contents using readlines() "
lines = fo.readlines()
print "Lines= ",lines
print "Size=",len(lines)
fo.close()
caps=0
small=0
print "Len \t Text "
for l in lines:
print len(l)-1,"\t",l
for i in range(len(l)-1):
if l[i].isupper():
caps=caps+1
if l[i].islower():
small=small+1
print "The number of uppercase characters = ",caps
print "The number of lowercase characters = ",small
print "Creating the text file "
fileCreation()
print "Displaying the file "
display_count_upper_lower_case()
"""
"""
********************************************************************************
****************************
# Program to count the number of lines staring with 'A'
# 1. Create Text File
# 2. Display the text file and
# 3. count the number of lines starting with 'A'

# 4. Exit
def fileCreation():
ofile = open("story.txt","w+")
choice = True
while True:
line = raw_input("enter sentence :")
ofile.write(line)
ofile.write('\n')
choice = raw_input("want to enter more data in file Y / N")
if choice.upper() == 'N' :
break
ofile.close()
def display_count_lines_starting_with_A():
fin = open("story.txt", "r")
print "\n Displaying the text file contents using readlines() "
lines = fin.readlines()
print "Lines= ",lines
print "Size=",len(lines)
fin.close()
count=0
print "Len \t Text "
for l in lines:
print len(l)-1,"\t",l
if l[0]=="A":
count=count+1
print "The number of lines starting with A = ",count
print "Creating the text file "
fileCreation()
print "Displaying the file "
display_count_lines_starting_with_A()
"""
"""
********************************************************************************
*
1.
#Program Code for creating a copy of existing file:
1. Create file.
2. Display the original file.
3. Copy the
def fileCreation():
ofile = open("story.txt","w+")
choice = True
while True:
line = raw_input("enter sentence :")
ofile.write(line)
choice = raw_input("want to enter more data in file Y / N")
if choice.upper() == 'N' : break
ofile.close()

def fileCopy():
ifile = open("story.txt","r")
#ifile.seek(0)
ofile = open("newstory.txt","w")
l = ifile.readline()
while l:
ofile.write(l)
l = ifile.readline()
ifile.close()
ofile.close()
def fileDisplaying1():
for l in open("story.txt","r").readlines():
print l
def fileDisplaying2():
for l in open("newstory.txt","r").readlines():
print l
print "\n Creating Original Text File : \n"
fileCreation()
print "\n Making Copy of the Text File \n"
fileCopy()
print "\n Displaying the Original File \n"
fileDisplaying1()
print "\n Displaying the Copy of the Text File \n"
fileDisplaying2()
"""
"""
********************************************************************************
****************************
2.
# File positions
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(44)
print "Read String is : ", str
# Check current position
position = fo.tell()
print "Current file position : ", position
# Reposition pointer at the beginning once again
position = fo.seek(0, 0)
str = fo.read(43)
print "Again read String is : ", str
# Close opened file
fo.close()
"""
Output >>>
Read String is : Python is a great language.
Yeah its great!!
Current file position : 43
Again read String is : Python is a great language.
Yeah its great!
>>>

"""
********************************************************************************
**************************

You might also like