0% found this document useful (0 votes)
3 views

pp-week-8

The document outlines three Python programming exercises focused on file handling. The first program generates and writes 20 random numbers to a file, the second demonstrates file pointer manipulation using seek(), tell(), and flush(), and the third illustrates different file reading methods: read(), readline(), and readlines(). Each program includes code snippets, outputs, and inferences on the importance of file handling techniques.

Uploaded by

23331a05e7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

pp-week-8

The document outlines three Python programming exercises focused on file handling. The first program generates and writes 20 random numbers to a file, the second demonstrates file pointer manipulation using seek(), tell(), and flush(), and the third illustrates different file reading methods: read(), readline(), and readlines(). Each program includes code snippets, outputs, and inferences on the importance of file handling techniques.

Uploaded by

23331a05e7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

‭PYTHON PROGRAMMING LAB‬

‭Week 9‬

‭Program 1: Writing 20 Random Numbers to a File‬


‭Aim:‬‭To generate 20 random numbers in the range of‬‭1 to 100 and write them to a file.‬


‭ escription:‬‭This program uses the random module to‬‭generate random numbers and writes‬
D

E7
‭them to a file named random_numbers.txt. The numbers are written line by line, ensuring each‬
‭number is stored separately for easy retrieval.‬

‭Program:‬

‭import random‬

‭for _ in range(20):‬
‭number = random.randint(1, 100)‬
‭file.write(str(number) + "\n")‬
05
‭with open("random_numbers.txt", "w") as file:‬
1A
‭print("20 random numbers have been written to 'random_numbers.txt'")‬

‭Output:‬
33

‭ 3‬
2
‭87‬
‭56‬
‭12‬
‭98‬
‭23

‭...‬

I‭nference:‬‭Writing random numbers to a file might‬‭seem simple, but it’s an essential step in‬
‭working with file handling.Open method in python can also be used but the choice to use with‬
‭open comes from the intuition to have code written in more pythonic way (as with open will‬
‭automatically close the file).A separate with open statement can be written to read to the‬
‭contents in file as well.‬
‭ rogram 2: Demonstrating seek(), tell(), and flush()‬
P
‭Methods‬
‭Aim:‬‭To illustrate the usage of seek(), tell(), and‬‭flush() methods for file pointer manipulation.‬

‭ escription:‬‭The program writes data to a file and‬‭demonstrates how to move the file pointer‬
D
‭using seek(), retrieve the current position using tell(), and forcefully write buffered data using‬
‭flush().‬


‭Program:‬

E7
‭with open("example.txt", "w") as file:‬
‭file.write("Hello, this is a test file.")‬
‭file.flush()‬

‭with open("example.txt", "r") as file:‬


‭print("Initial Position:", file.tell())‬
‭file.seek(7)‬

‭content = file.read(5)‬
‭print("Read content:", content)‬
‭print("Current Position:", file.tell())‬
05
‭print("After seek(7), Position:", file.tell())‬
1A
‭Output:‬

I‭nitial Position: 0‬
‭After seek(7), Position: 7‬
33

‭Read content: this‬


‭Current Position: 12‬

I‭nference:‬‭The seek() and tell() functions make navigating‬‭a file much easier, which is useful‬
‭23

‭when working with large files or when we need to edit specific portions of data. The flush()‬
‭function ensures that data is saved immediately, which is especially helpful in real-time‬
‭applications. These methods help us manage file operations more efficientl‬
‭ rogram 3: Demonstrating read(), readline(), and‬
P
‭readlines() Methods‬
‭Aim:‬‭To illustrate different file reading methods:‬‭read(), readline(), and readlines().‬

‭ escription:‬‭This program writes multiple lines to‬‭a file and demonstrates how different reading‬
D
‭methods retrieve data in various ways.‬

‭Program:‬


‭with open("sample.txt", "w") as file:‬

E7
f‭ile.write("Line 1: Hello World!\n")‬
‭file.write("Line 2: Python is great.\n")‬
‭file.write("Line 3: File handling is useful.\n")‬

‭with open("sample.txt", "r") as file:‬


‭print("Using read():")‬
‭file.seek(0)‬
‭print(file.read(10))‬
‭print("\nUsing readline():")‬
‭file.seek(0)‬
05
1A
‭print(file.readline()‬
‭print("\nUsing readlines():")‬
‭file.seek(0)‬
‭print(file.readlines())‬

‭Output:‬
33

‭ sing read():‬
U
‭Line 1: Hel‬

‭ sing readline():‬
U
‭23

‭Line 1: Hello World!‬

‭ sing readlines():‬
U
‭['Line 1: Hello World!\n', 'Line 2: Python is great.\n', 'Line 3: File handling is useful.\n']‬

I‭nference:‬‭Reading files is one of the most common‬‭tasks in programming, and Python gives us‬
‭different ways to do it. The read() method is useful when we need to fetch a specific number of‬
‭characters, readline() helps when we only want a single line, and readlines() makes it easy to‬
‭grab everything at once. Knowing when to use each of these makes working with files much‬
‭more efficient.‬

You might also like