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

File Handling Python

The document outlines two Python programs focused on file handling. The first program reads a text file and prints words of a specified length, while the second program sorts city names from a file alphabetically and writes them to a new file. Both programs illustrate essential file operations such as reading, writing, and processing data in Python.

Uploaded by

dinggunjan
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)
6 views4 pages

File Handling Python

The document outlines two Python programs focused on file handling. The first program reads a text file and prints words of a specified length, while the second program sorts city names from a file alphabetically and writes them to a new file. Both programs illustrate essential file operations such as reading, writing, and processing data in Python.

Uploaded by

dinggunjan
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

4.

8 File Handling

Aim: Develop a Python program that reads a text file and prints words of
specified lengths (e.g., three, four, five, etc.) found within the file.

Theory: File handling in Python is used to read data from files and
process it. The open() function is used to access a file in read mode. The file
content can be read using read() and separated into individual words using
split().
The length of each word can be checked using the len() function. With the
help of loops and conditional statements, the program filters and prints only
those words whose length matches the specified value. This demonstrates
how Python can analyze text data stored in files.

Code:
length = 5
file = open('[Link]', "r")
print("Words with length", length, ":")
for word in [Link]().split():
if len(word) == length:
print(word)
[Link]()

OUTPUT:
Conclusion: This program reads a text file and displays words of a
specified length. It demonstrates file reading, looping, and string processing
in Python.

4.9 File Handling

Aim: Write a python code to take a file which contains city names on each
line. Alphabetically sort the city names and write it in another file.

Theory: Python supports file handling operations that allow reading from
and writing to files. The file is opened in read mode to retrieve city names
stored line by line. These lines are stored in a list using splitlines().
The built-in sort() method arranges the city names alphabetically. The sorted
list is then written into another file using write mode. Displaying file
contents before and after sorting helps verify the correctness of the program
and
understand how file data is manipulated.

Code:
# Read cities from file
file = open("[Link]", "r")
cities = [Link]().splitlines()
[Link]()
print("\nOriginal Cities:")
for city in cities:
print(city)
# Sort cities
[Link]()
# Write sorted cities to new file
new_file = open("sorted_cities.txt",
"w") for city in cities:
new_file.write(city + "\
n") new_file.close()
print("\nSorted
Cities:") for city in
cities:
print(city)

OUTPUT:
Conclusion: This program successfully reads city names from a file,
displays them, sorts them alphabetically, and writes them into another file. It
demonstrates practical use of file handling and sorting operations in Python.

You might also like