Unit-IV Python_BCC402 (File Handling)
Unit-IV Python_BCC402 (File Handling)
UNIT-IV
@Dr. Shankar Thawkar
Introduction to File Handling in Python
• Relative path- Relative path is defined as the path related to the current working
directly(cwd). It starts at your current directory and never starts with a / .
• Python provides inbuilt functions for creating, writing and reading files.
• There are two types of files that can be handled in python:
• Normal text files
• Binary files (written in binary language,0s and 1s).
Text files:
In this type of file, Each line of text is terminated with a special character called EOL (End of
Line), which is the new line character (‘\n’) in python by default.
Binary files:
In this type of file, there is no terminator for a line and the data is stored after converting it into
machine understandable binary language.
• The key function for working with files in Python is the open() function.
Because "r" for read, and "t" for text are the default values, you do not need to specify them.
Note: Make sure the file exists, or else you will get an error.
“r” Read - Default value. Opens a file for reading, error if the file does not exist
“w” Write - Opens a file for writing, creates the file if it does not exist
“a” Append - Opens a file for appending, creates the specified file if it does not exist
“x” Create - Creates the specified file, returns an error if the file exists
f = open(“Myfile.txt", "r")
print(f.read()) Myfile.txt
f.close()
Note: You should always close your files, Hello! Welcome to Myfile.txt
in some cases, due to buffering, changes This file is for testing
made to a file may not show until you close purposes.
the file.
Good Luck!
Example
Loop through the file line by line:
Example
Create a file called "myfile.txt":
f = open("myfile.txt", "x")
Result: a new empty file is created!
Example
Create a new file if it does not exist:
f = open("myfile.txt", "w")
To write to an existing file, you must add a parameter to the open() function:
Output:
Hello! Welcome to demofile2.txt
This file is for testing purposes.
Good Luck!Now the file has more content!
Output:
Woops! I have deleted the content!
Note: the "w" method will overwrite the entire file.
@Dr. Shankar Thawkar
Write to an Existing File
• write() method is used to write a string to an already opened file.
• Strings may include numbers, special characters or other symbols.
• write() method does not add a newline(‘\n’) character to the end of
string.
Syntax:
file.write(string)
f=open(“Myfile.txt”,”w”);
lines=[“Hello world”, “Welcome to python”, “Enjoy Python”]
f.writelines(lines)
f.close()
print(“Data written to file”)
Output:
Data written to file
@Dr. Shankar Thawkar
Opening file using with keyword
• When a file is open using with keyword it is closed implicitly
(automatically) after use.
Output:
File renamed
import os
os.remove (“file1.txt”)
print(“File deleted”)
Output:
File deleted
@Dr. Shankar Thawkar
Assignment
• Write a program that read text from a file and write into another
file.
• Write a program that copy one file into another.
• Write a program that read a text file and display number of
vowels in a file.
• Write a program that read text from a file and write into another
file but in reverse order.