PPL Experiment No-8
PPL Experiment No-8
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:
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:
● 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).
● 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).
● For each specified word length in word_lengths, print the words that match that length.
Program:
import string
words = content.split()
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:
1.
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.