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

Python-U-4-ONE-SHOT-Notes

The document outlines the syllabus and important questions for Unit 4 of a Python course, focusing on file operations such as reading and writing files, file handling advantages and disadvantages, and various file opening modes. It includes programming tasks that require students to implement file operations, manipulate data, and handle user input. Additionally, it provides examples and explanations of Python functions related to file handling, such as read(), readline(), and seek().
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)
12 views

Python-U-4-ONE-SHOT-Notes

The document outlines the syllabus and important questions for Unit 4 of a Python course, focusing on file operations such as reading and writing files, file handling advantages and disadvantages, and various file opening modes. It includes programming tasks that require students to implement file operations, manipulate data, and handle user input. Additionally, it provides examples and explanations of Python functions related to file handling, such as read(), readline(), and seek().
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/ 70

AKTU B.

Tech III-Sem

Python
Crash Course
Unit-4 in One Shot Pragya Ma'am
SYLLABUS OF UNIT 4
Python File Operations: Reading files, Writing files in python, Understanding read functions, read(),
readline(), readlines(). Understanding write functions, write() and writelines() Manipulating file
pointer using seek Programming, using file operations.

1
IMPORTANT QUESTION OF UNIT 4
1. What is file handling? Explain the advantages and disadvantages and write the file opening modes

2. Write the different ways in which read and write operation are performed I python

3. Write a program to read a file line by ine and store it in


1.Varaible
2.list
4. Write a program to read first n lines
Or
Write a program to read first n lines
5. Construct a program which accepts a sequence of words separated by whitespace as file input. Print the
words composed of digits only.(AKTU 2023-24 ODD)

6 Write a program that a take space separated words and convert the digit into English word
Like
Hello 1 2
Output hello one two
2
7. There is a file named input.txt .Enter some positive number into the file name input.txt. Read the
content of the file and if it is an odd number write it to odd.txt and if number is even the write it to
the even.txt
8. write a program to read a file and capitalize first letter of every word in a file

9. Write a python program to count the frequency of words in a file

10. Write a program to copy the content of one file to another

11. Construct the python program to count the number of letter and digits in given input string into a
file object

12. Construct the python program to count the number of letter and digits in given input string into a
file object

3
AKTU B.Tech Second Year
PYTHON PROGRAMMING
UNIT 4
Python Program Flow Control Conditional blocks
LEC-1

Today’s Target

AKTU PYQs By
Pragya Rajvanshi

1
Advantages of File Handling in Python
FILES
1.Versatility: File handling in Python allows you to
 A file is a container in computer storage
perform a wide range of operations, such as
devices used for storing data.
creating, reading, writing, appending, renaming, and
 When a program is terminated, the entire
deleting files.
data is lost.
2. Flexibility: File handling in Python is highly
 Storing in a file will preserve your data even
flexible, as it allows you to work with different file
if the program terminates.
types (e.g. text files, binary files, CSV files etc.), and
 If you have to enter a large number of data,
to perform different operations on files (e.g. read,
it will take a lot of time to enter them all.
write, append, etc.).
 However, if you have a file containing all
CSV files are plain text files that store tabular data in
the data, you can easily access the contents
a simple format where each line represents a row,
of the file using a few commands .
and values within the row are separated by commas
2
3. User–friendly: Python provides a user-friendly Complexity : File handling in Python can be
interface for file handling, making it easy to complex, especially when working with more
create, read, and manipulate files advanced file formats or operations. Careful
Disadvantages of File Handling in Python attention must be paid to the code to ensure that
1.Error-prone: File handling operations in Python files are handled properly and securely.
can be prone to errors, especially if the code is Performance : File handling operations in Python
not carefully written or if there are issues with can be slower than other programming
the file system (e.g. file permissions, file locks, languages, especially when dealing with large
etc.). files or performing complex operations.
2.Security risks: File handling in Python can also
pose security risks, especially if the program
accepts user input that can be used to access or
modify sensitive files on the system. 3
OPEN () File opening modes
The Python open() function is used to open 1.r: open an existing file for a read operation.
internally stored files. 2 w: open an existing file for a write operation. If
It returns the contents of the file as Python the file already contains some data, then it will be
objects. overridden but if the file is not present then it
Python open() Function Syntax creates the file as well.
Syntax: open(file_name, mode) 3.a: open an existing file for append operation. It
Parameters:. won’t override existing data.
file name: the name of the file that we want to 4. r+: To read and write data into the file. This
open. mode does not override the existing data, but you
mode: This parameter is a string that is used to can modify the data starting from the beginning
specify the mode in which the file is to be of the file.
opened. 4
5 w+: To write and read data. It overwrites the 10. wb+ Open the file for reading and writing
previous file if one exists, it will truncate the file to in binary format. Truncates the file if it
zero length or create a file if it does not exist. already exists. Creates a new file if it does not
6.a+: To append and read data from the file. It won’t exist.
override existing data. 11.ab Open the file for appending in binary
7. rb : Open the file for reading in binary format. format. Inserts data at the end of the file.
Raises an I/O error if the file does not exist. Creates a new file if it does not exist.
8.rb+: Open the file for reading and writing in binary 12. ab+ Open the file for reading and
format. Raises an I/O error if the file does not exist appending in binary format. Inserts data at
9. wb: Open the file for writing in binary format. the end of the file. Creates a new file if it does
Truncates the file if it already exists. Creates a new not exist.
file if it does not exist
5
Open a file the to read the content Open a file to Write the content

Open a file to append the data

6
W+ mode example

7
8
Without seek ()

9
r+ mode

10
11
a+ mode

12
Different ways read can be performed
Read()

13
Different ways read can be performed
readline()

14
Different ways read can be performed
readlines()

15
AKTU B.Tech Second Year
PYTHON PROGRAMMING
UNIT 4
Python Program Flow Control Conditional blocks
LEC-2

Today’s Target

AKTU PYQs By
Pragya Rajvanshi

1
Read() Readline() Readlines()

• The read method  The readline method reads a The readlines method reads
reads the entire single line from a file and returns the entire contents of a file
contents of a file and it as a string. and returns it as a list of
returns it as a string.  This means that if you use strings, where each element of
• content = file.read() readline, you can read the the list is a single line of the
contents of a file line by line, file
which can be useful for processing file = open("filename.txt","r")
large files that do not fit in file.readlines()
memory
file = open("filename.txt", "r")
file.readline()
2
Write in a file Close a file
There are two ways to write in a file.
close() function closes the file and frees the
write() : Inserts the string str1 in a single
memory space acquired by that file. It is
line in the text file.
used at the time when the file is no longer
File_object.write(str1)
needed or if it is to be opened in a different
writelines() : For a list of string elements,
file mode.
each string is inserted in the text file. Used
Syntax: File_object.close()
to insert multiple strings at a single time.
File_object.writelines(L)
for L = [str1, str2, str3]

3
4
5
1write a python program to read a file line by line store it into a list

6
Write a Python program to read an entire text file

7
Write a Python program to read a file line by line store it into a variable.

8
Write a Python program to read first n lines of a file

9
Write a Python program to read last n lines of a file

10
Write a python program to find the longest words.

11
12
AKTU B.Tech Second Year
PYTHON PROGRAMMING
UNIT 4
Python file operations
LEC-3

Today’s Target
 File program
 AKTU PYQs
By
Pragya Rajvanshi

1
Construct a program which accepts a sequence of words separated by whitespace as file
input. Print the words composed of digits only.(AKTU 2023-24 ODD)

2
3
Write a program that a take space separated words and convert the digit into English word
Like
Hello 1 2
Output hello one two

4
5
There is a file named input.txt .Enter some positive number into the file name input.txt. Read the
content of the file and if it is an odd number write it to odd.txt and if number is even the write it to
the even.txt

6
7
Write a python program to find the longest words.

8
9
AKTU B.Tech Second Year
PYTHON PROGRAMMING
UNIT 4
Python file operations
LEC-4

Today’s Target
 File program
 AKTU PYQs
By
Pragya Rajvanshi

1
Example:

2
Construct a program to change the content of the fie by separating each character by
comma

3
4
What will be the output:

5
What will be the output:

6
Write a python program to count the number of lines in a text file.

7
write a program to read a file ABC.TXT AND count the line word character in a file.

8
write a program to read a file and capitalize first letter of every word in a file

9
Write a python program to count the frequency of words in a file

10
AKTU B.Tech Second Year
PYTHON PROGRAMMING
UNIT 4
Python file operations
LEC-5

Today’s Target
 File program
 AKTU PYQs
By
Pragya Rajvanshi

1
Write a program to copy the content of one file to another

2
Write a program to copy the content of one file to another

3
Write a program to read the random line in a file

4
Write a python program to asses if file is closed or not

5
Example of zip()

6
Write a python program to combine each line from first file wit the corresponding line in second
file

7
Write a python program to combine each line from first file wit the corresponding line in second
file

8
Write a python code to create a file with p.txt name and write your name and a father name in
this file and read this file to print it

9
Write a python program to get the file size of a plane file

10
Write a program to remove newline character from file

11
12
Construct the python program to count the number of letter and digits in given input string into a
file object

13
AKTU B.Tech Second Year
PYTHON PROGRAMMING
UNIT 4
Python file operations
LEC-6

Today’s Target
 File program
 AKTU PYQs
By
Pragya Rajvanshi

1
Seek() The reference point is selected by
the from_what argument. It accepts three
In Python, seek() function is used to change the
values:
position of the File Handle to a given specific
0: sets the reference point at the beginning of
position. File handle is like a cursor, which
the file
defines from where the data has to be read or
1: sets the reference point at the current file
written in the file.
position
Syntax: f.seek(offset, from_what), where f is file
2: sets the reference point at the end of the file
pointer
Parameters: By default from_what argument is set to 0.
Offset: Number of positions to move forward
from_what: It defines point of reference.
Returns: Return the new absolute position.

2
How do you use seek() to move write position to the
beginning of file.

3
How do you use seek() to reset the read/write position to specific location

4
How do you use seek() to reset the read position relative to current position

5
How do you use seek() to reset the write position relative to the current position

You might also like