Exp - 4
Exp - 4
Experiment No.04
A.1 Aim:
To explore Files and directories
a. Python program to append data to existing file and then display the entire file
A.2 Prerequisite:
1. Python basics
A.3 Outcome:
After successful completion of this experiment students will
be able to
1 r
Opens a file for reading only. The file pointer is placed at the beginning of the
file. This is the default mode.
2 rb
Opens a file for reading only in binary format. The file pointer is placed at the
beginning of the file. This is the default mode.
3 r+
Opens a file for both reading and writing. The file pointer placed at the beginning
of the file.
4 rb+
Opens a file for both reading and writing in binary format. The file pointer placed
at the beginning of the file.
5 w
Opens a file for writing only. Overwrites the file if the file exists. If the file does
not exist, creates a new file for writing.
6 wb
Opens a file for writing only in binary format. Overwrites the file if the file
exists. If the file does not exist, creates a new file for writing.
7 w+
Opens a file for both writing and reading. Overwrites the existing file if the file
exists. If the file does not exist, creates a new file for reading and writing.
8 wb+
Opens a file for both writing and reading in binary format. Overwrites the
existing file if the file exists. If the file does not exist, creates a new file for
reading and writing.
9 a
Opens a file for appending. The file pointer is at the end of the file if the file
exists. That is, the file is in the append mode. If the file does not exist, it creates a
new file for writing.
10 ab
Opens a file for appending in binary format. The file pointer is at the end of the
file if the file exists. That is, the file is in the append mode. If the file does not
exist, it creates a new file for writing.
11 a+
Opens a file for both appending and reading. The file pointer is at the end of the
file if the file exists. The file opens in the append mode. If the file does not exist,
it creates a new file for reading and writing.
12 ab+
Opens a file for both appending and reading in binary format. The file pointer is
at the end of the file if the file exists. The file opens in the append mode. If the
file does not exist, it creates a new file for reading and writing.
1 file.closed
Returns true if file is closed, false otherwise.
2 file.mode
Returns access mode with which file was opened.
3 file.name
Returns name of the file.
4 file.softspace
Returns false if space explicitly required with print, true otherwise.
Example
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
fo.close()
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
fo.close()
The above method would create foo.txt file and would write given content in that file and
finally it would close that file. If you would open this file, it would have following content.
Python is a great language.
Yeah its great!!
The read() Method
The read() method reads a string from an open file. It is important to note that Python strings
can have binary data. apart from text data.
Syntax
fileObject.read([count]);
Here, passed parameter is the number of bytes to be read from the opened file. This method
starts reading from the beginning of the file and if count is missing, then it tries to read as much
as possible, maybe until the end of file.
Example
Let's take a file foo.txt, which we created above.
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
position = fo.tell();
fo.close()
#!/usr/bin/python
import os
#!/usr/bin/python
import os
os.remove("text2.txt")
Directories in Python
All files are contained within various directories, and Python has no problem handling these
too. The os module has several methods that help you create, remove, and change directories.
The mkdir() Method
You can use the mkdir() method of the os module to create directories in the current directory.
You need to supply an argument to this method which contains the name of the directory to be
created.
Syntax
os.mkdir("newdir")
Example
Following is the example to create a directory test in the current directory −
#!/usr/bin/python
import os
os.mkdir("test")
#!/usr/bin/python
import os
os.chdir("/home/newdir")
#!/usr/bin/python
import os
os.getcwd()
#!/usr/bin/python
import os
os.rmdir( "/tmp/test" )
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 Blackboard
or emailed to the concerned lab in charge faculties at the end of the
practical in case the there is no Black board access available)
Grade :
Part:- A
def append_to_file(file_name, data):
try:
file.write(data + '\n')
def display_file_content(file_name):
try:
content = file.read()
print("File Content:")
print(content)
except Exception as e:
file_name = 'C33.txt'
append_to_file(file_name, data_to_append)
display_file_content(file_name)
Part:- B
def count_file_stats(file_name):
with open(file_name, 'r') as file:
lines = file.readlines()
num_lines = len(lines)
file_name = 'C33.txt'
Appending data to a file and then displaying its contents showcases file handling
B.4 Conclusion:
We can display the data in existing file and can insert the new data in it and display
it with number of lines and characters
B.5 Question of Curiosity
ANS:-