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

PPL Experiment No-8

The document outlines an experiment for a Python programming course focused on file operations, exception management, and creating executable files. It includes a detailed problem statement for extracting words of specified lengths from a text file, along with a sample program and steps to build an executable using PyInstaller. The conclusion emphasizes the understanding of basic file handling and exception management in Python.
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)
9 views7 pages

PPL Experiment No-8

The document outlines an experiment for a Python programming course focused on file operations, exception management, and creating executable files. It includes a detailed problem statement for extracting words of specified lengths from a text file, along with a sample program and steps to build an executable using PyInstaller. The conclusion emphasizes the understanding of basic file handling and exception management in Python.
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/ 7

Subject: Python

Programming (VSEC202)
Academic Year: 2024 – 2025
Year: Sem: II
FE

Experiment No 8
Aim: Write python programs to handle file operations, manage exceptions, and create
Python packages and executable files effectively.

Tools Used:
1. Python 3.x interpreter
2. Text editor or Integrated Development Environment (IDE) such as VS Code, PyCharm, or
IDLE
3. (Optional) Compiler for C to compare constructs

Problem Statement 1: Extracting Words from Text File*: 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.

To develop a Python program that reads a text file and prints words of specified lengths, you can
follow the approach below:

Steps:

1. Open and read the contents of the file.


2. Split the text into words.
3. Filter the words based on the specified length.
4. Print the filtered words.

Functions used:

file_path: This is the path to your text file. Replace 'sample.txt' with the actual path of your file.

word_lengths: This is a list of word lengths you're interested in, for example, [3, 4, 5] to find
words of length 3, 4, and 5.
Subject: Python
Programming (VSEC202)
Academic Year: 2024 – 2025
Year: Sem: II
FE
Word splitting: The text is split into words using the .split() method. It works by splitting on
any whitespace.

Cleaning words: The code cleans each word by removing any punctuation (i.e., only
alphanumeric characters remain).

Filtering words: The words are then filtered based on the specified lengths and stored in a
dictionary, which organizes them by their lengths.

Flowchart:

Start: The program begins execution.

Input File Path and Word Lengths:

● Take the file_path and word_lengths (list of integers) as inputs.

Open the Text File:

● Open the file specified by the file_path for reading.

Read Content from File:

● Read the entire content of the file into a variable (text).

Split Text into Words:

● Split the text into words using the split() function, which splits based on spaces.
Subject: Python
Programming (VSEC202)
Academic Year: 2024 – 2025
Year: Sem: II
FE
Initialize Word Length Filter Dictionary:

● Create a dictionary (or similar structure) to store words categorized by their lengths (for
the specified lengths).

Iterate Over Words:

● For each word in the list of words:


○ Remove Punctuation: Clean each word by removing punctuation characters
(keeping only alphanumeric characters).
○ Check Length of Word: Check if the cleaned word's length matches any of the
specified lengths in word_lengths.

Store Matching Words:

● If the word matches one of the specified lengths, store it in the corresponding category in
the dictionary (e.g., words of length 3, 4, or 5).

Print Words by Length:

● For each specified word length in word_lengths, print the words that match that length.

End: The program finishes execution.


Subject: Python
Programming (VSEC202)
Academic Year: 2024 – 2025
Year: Sem: II
FE

Program:

import string

def extract_words_of_specified_lengths(file_path, lengths):


"""
Extract words from a file that have specified lengths.

:param file_path: Path to the text file.


:param lengths: List of lengths of words to extract.
"""
try:

with open(file_path, 'r') as file:


content = file.read()

content = content.translate(str.maketrans('', '', string.punctuation))

words = content.split()

word_dict = {length: [] for length in lengths}

for word in words:


word_length = len(word)
if word_length in lengths:
word_dict[word_length].append(word)

for length, words_of_length in word_dict.items():


print(f"Words of length {length}:")
print(", ".join(words_of_length) if words_of_length else "No words found.")
print()
Subject: Python
Programming (VSEC202)
Academic Year: 2024 – 2025
Year: Sem: II
FE
except FileNotFoundError:
print(f"Error: The file at {file_path} was not found.")
except Exception as e:
print(f"An error occurred: {e}")

file_path = '"C:\Users\Rajashri\Downloads\APSIT\CSE(Data Science)\AY 2024-25 Even Sem\


FE\abc.txt"abc.txt' # Replace with your actual file path
lengths = [3, 4, 5] # Specify the lengths of words you want to extract
extract_words_of_specified_lengths(file_path, lengths)

Output -
Subject: Python
Programming (VSEC202)
Academic Year: 2024 – 2025
Year: Sem: II
FE

Problem Statement 2: Building an Executable File*: Create a executable file for any program
developed in earlier practical.

Steps:

Install PyInstaller using pip:


bash
Copy
pip install pyinstaller

1.

Write your Python program (e.g., hello.py):


Subject: Python
Programming (VSEC202)
Academic Year: 2024 – 2025
Year: Sem: II
FE
python
Copy
print("Hello, World!")

2.

Create the executable: In the terminal, navigate to the folder containing hello.py, then run:
bash
Copy
pyinstaller --onefile hello.py

3.
4. Result: The executable file will be created in the dist/ folder. You can find hello.exe
there.

Program-

Output-

Conclusion: Understood the basic file handling operations, exceptions handling and use of
packages.

You might also like