[go: up one dir, main page]

0% found this document useful (0 votes)
69 views10 pages

Exp 6 IPP

This document provides instructions for Experiment 6 of an Introduction to Python Programming course. It discusses file handling operations like reading and writing to files. Specifically, it covers opening files, reading file content including specific lines or characters, writing to existing files by appending or overwriting, and creating new files. The key methods covered are open(), read(), readline(), write(), and close(). Examples are given to demonstrate reading the first 5 characters of a file, reading line by line, overwriting file content, and creating a new file.

Uploaded by

Shreyas Desai
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)
69 views10 pages

Exp 6 IPP

This document provides instructions for Experiment 6 of an Introduction to Python Programming course. It discusses file handling operations like reading and writing to files. Specifically, it covers opening files, reading file content including specific lines or characters, writing to existing files by appending or overwriting, and creating new files. The key methods covered are open(), read(), readline(), write(), and close(). Examples are given to demonstrate reading the first 5 characters of a file, reading line by line, overwriting file content, and creating a new file.

Uploaded by

Shreyas Desai
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/ 10

SVKM’s NMIMS

Mukesh Patel School of Technology Management & Engineering


Computer Science and Engineering (Data Science) Department
Program: B.Tech. Semester: III

Course: Introduction to Python Programming


List of Experiments

Faculty:Prof. Ami Munshi w.e.f. July 2022

Exp Title Prerequisite* CO#


No.
1 Operators (comparison, logical, bitwise, Knowledge of 1
arithmetic) operators
a) Construct a simple calculator using
arithmetic operations
b) Develop a program to check if two
numbers are equal, less than or greater
than.
c) Write a program to implement logic
gates (AND, OR, NOT) gates
2. Data structures (string, list, tuple) Understanding of 1
a. Give new name to the user by exchanging strings, lists and
first and last characters of his/her name tuple
b. Teacher has entered math marks for all the
students in a list. Help her to print the
second highest and second lowest marks.
Use list data structure.
c. Teacher has to select all the students having
roll numbers, that are multiple of 5. Help
the teacher. Use list data structure
d. For a given number A which contains only
digits 0's and 1's, if it is possible to make all
digits same by flipping only one digit, print
YES, else print NO
e. Student names are saved in a tuple. Find the
length of each student name.
3. Data Structures (Set and Dictionary) Understanding of 1
a. Take a paragraph from some book. Create a dictionary and set
dictionary consisting of all the words and its
frequency in the given text.
b. Create a dictionary from following two lists.
The first list will have name of the students and
second list will have their corresponding marks
c. Implement Caesar cipher using Python
dictionary
d. Create set S1 will all even numbers from 1 to
100. Create set S2 with all squares of numbers
1 to 10. Create the following sets from these
two sets
i. Union of S1 and S2
ii. Intersection of S1 and S2
4 Conditional statements and Loops Knowledge of 3
a. Given an integer k, perform the following task applying
using if-else statements conditional
a. Print whether k is negative or positive statements and
b. Print whether k is even or odd loops
c. Print whether k is multiple of three
b. For an examination conducted in a university,
grade the marks obtained by the students as
High, Medium, Low based on the following
criteria
i. High: 75< Marks<=100
ii. Medium: 50<Marks<=75
iii. Low: Marks<=50
c. Geeta and Seeta are playing a number game,
where for every iteration a random number (1
to 50) will be generated first for Geeta and then
for Seeta. Input the number of iterations to be
played from the user. They get points as
follows:
i. For every even number, 2 point
ii. For every odd number, 3 points
iii. For every square number (even or odd),
4 points
Print the points obtained by Geeta and Seeta
5 Functions
a. Calculate the Greatest Common Divisor with
gcd() function and by writing your own
function
b. Write two functions sub  and mul. 
i. mul function will take any
number,multiply it by 3 and return.
ii. sub function will take an integer as an
input, multiply that number by 3 by
calling mul function, subtract 2 from it
and return the value
c. Create a function name student with his default
information as his standard and school name
with provision that if student wants can change
default information
d. Create a lamda function to generate square
roots for the numbers given in the list

6 File Handling
a. Read all the content present inside the file
i. Read only the first 5 characters of the
file.
ii. Read the content of the file on a line by
line basis.
iii. Read all the lines present inside the text
file including the newline characters.
iv. ReadaspecificlinefromaFile
b. Write content in a file
i. Write the String ‘Hello World’ into the
‘test.txt’ file
ii. Write “Hello World” in first line and
“Hello Python” in second line
iii. Write list of fruits in .txt file
d. Write a program to read resume and its details
7
8
9
10
11

* Students are expected to be ready with the prerequisite before attending the lab
Experiment No.06
PART A
(PART A: TO BE REFFERED BY STUDENTS)
Experiment 6
A.1 Aim: File Handling
a. Read all the content present inside the file
v. Read only the first 5 characters of the file.
vi. Read the content of the file on a line by line basis.
vii. Read all the lines present inside the text file including the newline characters.
viii. ReadaspecificlinefromaFile
b. Write content in a file
iv. Write the String ‘Hello World’ into the ‘test.txt’ file
v. Write “Hello World” in first line and “Hello Python” in second line
vi. Write list of fruits in .txt file
c. Write a program to read resume and its details
A.2 Prerequisite:
Reading and writing from different types of files
A.3 Learning Outcome:
After completing this exercise you will be able to-
1. Develop a small application to read content of a file
2. Implement a program to write applications in the file

A.4 Theory:

A.4.1 File Handling-


The file handling plays an important role when the data needs to be stored permanently into the
file. A file is a named location on disk to store related information. We can access the stored
information (non-volatile) after the program termination.

In Python, files are treated in two modes as text or binary. The file may be in the text or binary
format, and each line of a file is ended with the special character.

Hence, a file operation can be done in the following order.

o Open a file
o Read or write - Performing operation
o Close the file

A.4.2 Opening a file


Python provides an open() function that accepts two arguments, file name and access mode in
which the file is accessed. The function returns a file object which can be used to perform
various operations like reading, writing, etc.

file object = open(<file-name>, <access-mode>)     

There are four different methods (modes) for opening a file:

"r" - Read - Default value. Opens a file for reading, error if the file does not exist

"a" - Append - Opens a file for appending, creates the file if it does not exist

"w" - Write - Opens a file for writing, creates the file if it does not exist

"x" - Create - Creates the specified file, returns an error if the file exists

In addition you can specify if the file should be handled as binary or text mode

"t" - Text - Default value. Text mode

"b" - Binary - Binary mode (e.g. images)

If the file is located in the same folder-

f = open("demofile.txt", "r")
print(f.read())

If the file is located at a different location, specify the path-

f = open("D:\\myfiles\welcome.txt", "r")
print(f.read())

Read Only Parts of the File

Return the 5 first characters of the file:

f = open("demofile.txt", "r")
print(f.read(5))

Read Lines

You can return one line by using the readline() method:

f = open("demofile.txt", "r")
print(f.readline())

By looping through the lines of the file, you can read the whole file, line by line:
f= open("demofile.txt", "r")
for x in f:
  print(x)

A.4.2 Close Files


Close the file when you are finish with it:
f= open("demofile.txt", "r")
print(f.readline())
f.close()

A.4.3 Write to an Existing File

To write to an existing file, you must add a parameter to the open() function:

"a" - Append - will append to the end of the file

"w" - Write - will overwrite any existing content

f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()

#open and read the file after the appending:


f = open("demofile2.txt", "r")
print(f.read())

Overwrite the content:

f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()

#open and read the file after the appending:


f = open("demofile3.txt", "r")
print(f.read())

A.4.4 Create a New File

To create a new file in Python, use the open() method, with one of the following parameters:

"x" - Create - will create a file, returns an error if the file exist

"a" - Append - will create a file if the specified file does not exist
"w" - Write - will create a file if the specified file does not exist

Create a file called "myfile.txt":

f = open("myfile.txt", "x")

A.5 Task to be completed:

a. Read all the content present inside the file


ix. Read only the first 5 characters of the file.
x. Read the content of the file on a line by line basis.
xi. Read all the lines present inside the text file including the newline characters.
xii. ReadaspecificlinefromaFile
b. Write content in a file
vii. Write the String ‘Hello World’ into the ‘test.txt’ file
viii. Write “Hello World” in first line and “Hello Python” in second line
ix. Write list of fruits in .txt file
c. Write a program to read resume and its details

***************************************************************************

PART B
(PART B: TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the practical.
The soft copy must be uploaded on the MS Teams assignment link at the end of the practical)
Roll No. L012 Name: Shreyas Desai
Program : CSDS 311 Division: L
Batch: B1 Date of Experiment:
Date of Submission: Grade :

B.1 Tasks given in PART A to be completed here


(Students must write the answers of the task(s) given in the PART A /Students must copy the
code, output screenshots here based on the task(s) given in section A.5)
1:

2:
3:

B.2 Observations and Learning:


(Students must write the observations and learning based on their understanding built about the
subject matter and inferences drawn)
 I observed and learnt how to read specific characters from a line.
 I learned how to read a specific line number from a file.
 I learnt how to write and create a new file.
 I observed the difference between writing and appending in the file.

B.3 Conclusion:
(Students must write the conclusive statements as per the actual attainment of individual
outcomes listed above and learning/observation noted in section B.2)
 I was able to develop a small application to read content of a file
 I could implement a program to write applications in the file

B.4 Question of curiosity:


(This section aims to develop student’s curiosity, research ability. So students are free to search
the answer on web. This section includes general .case study based/out of syllabus questions
based on the theme of the experiment. Students should be able to answer these questions based
the experiment carried out.)

Q1. To read all contents from file object FILE at once we may use

a) FILE.read(*)
b) FILE.readlines()
c) FILE.read()
d) FILE.readline()

Q2. To read 20 characters from file object FILE at once we may use

a) FILE.read(20)
b) FILE.readlines(20)
c) FILE.read(char=20)
d) FILE.readline(char=20)

Q3. Which of the following file mode will refer to the BINARY mode?

a) binary
b) b
c) bin
d) w

Q4. Which of the following function is used to write LIST OF STRINGS in a file?
a) write()
b) writeline()
c) writelines()
d) write(all)

Q5 If we want to add more contents in an existing file, file must be opened in............mode
a) binary
b) append
c) write
d) it is not possible

You might also like