0% found this document useful (0 votes)
35 views7 pages

Text File Operations and Analysis

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views7 pages

Text File Operations and Analysis

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

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("[Link]","w+")
choice = True
while True:
line = raw_input("enter sentence :")
[Link](line)
[Link]('\n')
choice = raw_input("want to enter more data in file Y / N")
if [Link]() == 'N' : break
[Link]()
def display_text_file():
fo = open("[Link]", "r")
print "\n Displaying the text file contents using readlines() "
lines = [Link]()
[Link]()
for l in lines:
print l
def count_word():
fo = open("[Link]", "r")
print "\n Displaying each character of the the text file in separate lines
"
lines = [Link]()
word1=raw_input("Enter first word to be counted : ")
word2=raw_input("Enter second word to be counted : ")
[Link]()
count1=0
count2=0
for l in lines:
for w in [Link]():
if [Link]()==[Link]():
count1=count1+1
if [Link]()==[Link]():
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("[Link]","w+")
choice = True
while True:
line = raw_input("enter sentence :")
[Link](line)
[Link]('\n')
choice = raw_input("want to enter more data in file Y / N")
if [Link]() == 'N' : break
[Link]()
def display_text_file():
fo = open("[Link]", "r")
print "\n Displaying the text file contents using readlines() "
lines = [Link]()
[Link]()
for l in lines:
print l
def count_alphabets():
fo = open("[Link]", "r")
print "\n Displaying each character of the the text file in separate lines
"
lines = [Link]()
alpha1=raw_input("Enter first alphabet to be counted : ")
alpha2=raw_input("Enter second alphabet to be counted : ")
[Link]()
count1=0
count2=0
for l in lines:
for w in [Link]():
for c in w:
if [Link]()==[Link]():
count1=count1+1
if [Link]()==[Link]():
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("[Link]","w+")
choice = True
while True:
line = raw_input("enter sentence :")
[Link](line)
[Link]('\n')
choice = raw_input("want to enter more data in file Y / N")
if [Link]() == 'N' : break
[Link]()
def display_text_file():
fo = open("[Link]", "r")
print "\n Displaying the text file contents using readlines() "
lines = [Link]()
[Link]()
for l in lines:
print l
def display_each_char_in_separate_lines():
fo = open("[Link]", "r")
print "\n Displaying each character of the the text file in separate lines
"
lines = [Link]()
[Link]()
for l in lines:
for w in [Link]():
for ch in w:
print ch
def read_display_file_backwards():
fo = open("[Link]", "r")
print "\n Displaying the text file contents backwards "
lines = [Link]()
print "Lines : ", lines
size=len(lines)
print "Number of lines =",size
[Link]()
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("[Link]","w+")
choice = True
while True:
line = raw_input("enter sentence :")
[Link](line)
[Link]('\n')
choice = raw_input("want to enter more data in file Y / N")
if [Link]() == 'N' : break
[Link]()
def display_count_upper_lower_case():
fo = open("[Link]", "r")
print "\n Displaying the text file contents using readlines() "
lines = [Link]()
print "Lines= ",lines
print "Size=",len(lines)
[Link]()
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("[Link]","w+")
choice = True
while True:
line = raw_input("enter sentence :")
[Link](line)
[Link]('\n')
choice = raw_input("want to enter more data in file Y / N")
if [Link]() == 'N' :
break
[Link]()
def display_count_lines_starting_with_A():
fin = open("[Link]", "r")
print "\n Displaying the text file contents using readlines() "
lines = [Link]()
print "Lines= ",lines
print "Size=",len(lines)
[Link]()
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("[Link]","w+")
choice = True
while True:
line = raw_input("enter sentence :")
[Link](line)
choice = raw_input("want to enter more data in file Y / N")
if [Link]() == 'N' : break
[Link]()

def fileCopy():
ifile = open("[Link]","r")
#[Link](0)
ofile = open("[Link]","w")
l = [Link]()
while l:
[Link](l)
l = [Link]()
[Link]()
[Link]()
def fileDisplaying1():
for l in open("[Link]","r").readlines():
print l
def fileDisplaying2():
for l in open("[Link]","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("[Link]", "r+")
str = [Link](44)
print "Read String is : ", str
# Check current position
position = [Link]()
print "Current file position : ", position
# Reposition pointer at the beginning once again
position = [Link](0, 0)
str = [Link](43)
print "Again read String is : ", str
# Close opened file
[Link]()
"""
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!
>>>

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

Common questions

Powered by AI

The program uses readlines() to read and store file contents into a list, loop through each line to process content, split lines to handle words, and conditional statements to perform operations like counting specific words or letters . Additionally, file reading is reversed by iterating from the last line to the first and displaying each line’s characters in reverse order using slicing .

The program positions file pointers using tell() and seek() to track and reset reading positions. It first reads a block of text, checks the pointer position, resets to the beginning, and rereads to demonstrate how file pointers can control reading operations from specific positions, essential for sequential reads .

The program optimizes reading by using readlines() to load the file into memory as a list of lines, reducing repetitive I/O operations. It processes data using loops to avoid excess storage or computational overhead, allowing efficient data handling even for non-sequential tasks like reverse reading or character-by-character parsing .

The program reads each line of the text file and checks if the first character is 'A' (case-sensitive). It increments a counter for every line starting with 'A', displaying the result at the end . Such a method requires the assumption that the input format is consistent, focusing on practical constraints of simple file processing tasks.

The program allows users to input specific words or alphabets to count their occurrences. It reads lines from the file, splits them into words, iterates each word for alphabets, and compares them using a case-insensitive method. It maintains separate counts for each user-specified word or alphabet .

The program uses a while loop alongside raw_input() to facilitate continuous data entry, prompting the user repeatedly to input sentences until they signal completion by responding with 'N'. This method allows dynamic interaction and offers the user the choice to extend data entry flexibly .

The program uses a dedicated function to open the file in read mode and employ readlines() to store and display each line of the file. This enables users to view all content line-by-line in the same format as stored .

The program iterates over each character in the file, using isupper() and islower() methods to increment counts of uppercase and lowercase characters, respectively. It displays the total counts after reading the file .

The program allows a user to create a text file and add content by accepting sentences input through raw_input(). The user is prompted to enter more data, and lines are written to the file 'story.txt'. This process continues until the user chooses to stop by entering 'N'. Thus, the user can interactively build the content of the text file .

The program creates a copy of an existing file by opening 'story.txt' for reading and 'newstory.txt' for writing. It reads each line from the original file and writes it to the new file using a loop, then closes both files to finalize the copy process .

You might also like