File Handling Functions in Python :
This blog Explains File Handling Functions in Python and Examples in Here.
Python File Handling: Create, Open, Append, Read, Write
Python provides an inbuilt function for creating, writing, and reading files.
File Handling :
The key function for working with files in Python is the open()
function.
The open()
function takes two parameters; filename, and 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)
Syntax:
f = open(“demofile.txt”)
Open a File on the Server:
Text File is located on the server example :
Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!
To open the file, use the built-in open()
function.
The open()
function returns a file object, which has a read()
method for reading the content of the file:
f = open(“demofile.txt”, “r”)
print(f.read())
Output:-
C:\Users\My Name>python demo_file_open.py
Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!
If the file is located in a different location, you will have to specify the file path, like this
Examples:
f = open(“D:\\myfiles\welcome.txt”, “r”)
print(f.read())
Output :-
C:\Users\My Name>python demo_file_open_d.py
Welcome to this text file!
This file is located in a folder named “myfiles”, on the D drive.
Good Luck!
Read Only Parts of the File
By default the read() method returns the whole text, but you can also specify how many characters you want to return:
Example:
f = open(“demofile.txt”, “r”)
print(f.read(5))
Output:
Hello
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
Example
Create a file called “myfile.txt”:f = open(“myfile.txt”, “x”)
Result: a new empty file is created!
Write to an Existing File:
"a"
– Append – will append to the end of the file
"w"
– Write – will overwrite any existing content
Example:
Open the file “demofile2.txt” and append content to the file:
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())
C:\Users\My Name>python demo_file_append.py
Hello! Welcome to demofile2.txt
This file is for testing purposes.
Good Luck!Now the file has more content!
Python Delete File:
Delete a File :
To o delete a file, you must import the OS module, and run its os.remove()
function:
Example:
Remove the file “demofile.txt”:
import os
os.remove(“demofile.txt”)
Delete Folder :
To delete an entire folder, use the os.rmdir()
method
Remove the folder “myfolder”:import os
os.rmdir(“myfolder”)
Reference: