0% found this document useful (0 votes)
25 views4 pages

Python File Word Sorting and Stats

The document contains three Python programs: one for sorting words from a source file into an output file with all words in lowercase, another for printing each line of a file in reverse order, and a third for computing the number of characters, words, and lines in a file. Each program includes error handling for file not found and IO errors. Example usage is provided for each program with specified file names.

Uploaded by

gayathrid.1803
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)
25 views4 pages

Python File Word Sorting and Stats

The document contains three Python programs: one for sorting words from a source file into an output file with all words in lowercase, another for printing each line of a file in reverse order, and a third for computing the number of characters, words, and lines in a file. Each program includes error handling for file not found and IO errors. Example usage is provided for each program with specified file names.

Uploaded by

gayathrid.1803
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

UNIT-4

1. Aim :Write a program to sort words in a file and put


them in another [Link] output file should have only
lower case words,so any upper case words from
source must be lowered
PROGRAM:
def sort_words_in_file(source_file,output_file):
try:
with open(source_file,'r')as file:
words=[Link]().split()
words=[[Link]()for word in words]
[Link]()
with open(output_file,'w')as file:
for word in words:
[Link](f”{word}\n”)
print(f"words have been sorted and written
to{output_file}")
except File Not Found Error:
print(f"error:the file{source_file}was not found")
except IO Error as e:
print(f"error:an IOError occurred details:{e}")

#example usage
source_filename='[Link]'
output_filename='sorted_output.txt'
sort_words_in_file(source_filename,output_filename)

2. AIM:Python program to print each line of a file in


reverse order
PROGRAM:
def reverse_lines_in_file(file_path):
try:
with open(file_path,'r')as file:
for line in file:
reversed_line=[Link]()[::-1]
print(reversed_line)
except FileNotFoundError:
print(f"Error:the file{file_path}was not found")
except IOError as e:
print(f"Error:An IOError occurred Details:{e}")
file_path='[Link]'
reverse_lines_in_file(file_path)

3. AIM: Python program to compute the number of


characters,words and files
PROGRAM:
def compute_file_statistics(file_path):
try:
num_characters=0
num_words=0
num_lines=0
with open(file_path,'r')as file:
for line in file:
num_lines+=1
num_characters+=len(line)
num_words+=len([Link]())
print(f"Number of characters:{num_characters}")
print(f"Number of words:{num_words}")
print(f"Number of lines:{num_lines}")
except FileNotFoundError:
print(f"error:the file{file_path}was not found")
except IOError as e:
print(f"Error:an IOError occurred. Details{e}")
file_path='[Link]'
compute_file_statistics(file_path)

You might also like