File Handling in Python
File Handling in Python
Python provides built-in libraries for handling file functions like create, update, append, read and write.
Open (): Returns file object by allowing file opening action with file modes
Syntax : Open ( <file name> , < file modes >)
<file name> : Name of file
<file mode>: File mode specifies the certain actions need to be performed once file object is accessed
Example: Open file in read mode
>>>file=open("News.txt","r") >>>
File Modes
'r' | Default file mode for reading file content |
'w' | Write mode to write content in file. if file does’t exist, it create new file & overrides if exists. |
'x' | Create a new file. If file already exists, the operation fail. |
'a' | Append content if file exist & create a new file if it does not exist |
't' | Default mode for text file |
'b' | Binary mode if binary file needs to be opened |
'+' | Opens file in read and write mode |
This allows us to perform operations like reading, write, append on the file objects. Given below are a list of file modes available in the file object
Create a new file with “x” file mode
It is possible to create a new file using the open() function, the only thing required is of the addition of “x” file mode in file opening function followed by the new file name.
In below example, we are creating a new file called “Galaxy.txt” bypassing “x” in file mode
>>file=open("Galaxy.txt","x") >>>
read(): It reads the content from file
Syntax : <file object>.read()
In the below example, we have created a file object to open file “License.txt” in read “r” mode.
>>>file=open("License.txt","r") >>>str=file.read() >>>print(str)
We have used a read () method to read the contents of the “License.txt” file. variable “str” is storing the string contents using read() methods. we have used the print method to display output on the console. In our next example, we are using a read(n) method to read a fixed number of characters from the beginning of the file. read(10)~ is reading 10 characters from a file .
>>file=open("License.txt","r") >>>str=file.read(10) >>>print(str) A.History
readline() : It read contents by line
Syntax : < file object>.readline()
In the below example, we are using readline() method to read content by line . we have opened the “License.txt” file in read “r” mode to read its content. In the below example, it is returning a single line from the file.
>file=open("License.txt","r") >>>str=file.readline(10) >>>print(str) ..HISTORY OF THE SOFTWARE
To read all content from a file , it is essential to write a loop over file object. Refer below example for clear understanding.
>>file=open("License.txt","r") for x in file ...print(x)
write() : It write contents to the file file
syntax : <file object>.write(<“contents ”>)
In the below example, we have opened the “License.txt” file in write mode, we are writing strings in file with write() function. Write function takes string arguments/texts as an argument and writes to the file.
>>>file=open("News.txt","w") >>file.write("new content") 11 >>>file=open("News.txt","r") >>>str=file.read() >>>print(str) new content
To append content in an existing file, we are opening the file in append “a” mode. This will append new text to the end of line of existing file. In the below example, we are appending new text in the “News.txt” file & we are revalidating it by reading back the contents from the “New.txt” file.
>>>file=open("News.txt","a") >>file.write("This is appended text") >>>file=open("News.txt","r") >>>str=file.read() >>>print(str) new content This is appended text
close() : closes the file object
Syntax: <file object>.close()
In the below example, after completing read and write operation on a file . we closed the file object with a close() method.
>>>file=open("News.txt","w") >>file.write("New Content") 11 >>>file=open("News.txt","r") >>>str=file.read() >>>print(str) new content >>>file.close()