file handling in python
python File handling System
File Handling in Python
Python too supports file handling and allows users to handle files
i.e., to read and write files, along with many other file handling options, to operate on files.
Working of open() function
There are three kinds of mode, that Python provides and how files can be opened:
- “ r “ :- for reading.
- “ w “ :- for writing.
- “ a “ :- for appending.
- “ r+ “ :- for both reading and writing
# a file named "geek", will be opened with the reading mode.
file = open('geek.txt', 'r')
# This will print every line one by one in the file
for each in file:
print (each)
Working of read() mode
# Python code to illustrate read() mode
file = open("file.text", "r")
print file.read()
Another way to read a file is to call a certain number of characters like in the following code the interpreter will read the first five characters of stored data and return it as a string:
# Python code to illustrate read() mode character wise
file
=
open
(
"file.txt"
,
"r"
)
print
file
.read(
5
)
Creating a file using write() mode
To manipulate the file, write the following in your Python environment:
# Python code to create a file
file = open('geek.txt','w')
file.write("This is the write command")
file.write("It allows us to write in a particular file")
file.close()
The close() command terminates all the resources in use and frees the system of this particular program.
Working of append() mode
# Python code to illustrate append() mode
file = open('geek.txt','a')
file.write("This will add this line")
file.close()
It is designed to provide much cleaner syntax and exceptions handling when you are working with code. That explains why it’s good practice to use them with a statement where applicable. This is helpful because using this method any files opened will be closed automatically after one is done, so auto-cleanup.
Example:
# Python code to illustrate with()
with open("file.txt") as file:
data = file.read()
# do something with data
Using write along with with() function
We can also use write function along with with() function:# Python code to illustrate with() alongwith write()
with open("file.txt", "w") as f:
f.write("
Hello World!!!"
)
split() using file handling
We can also split lines using file handling in Python. This splits the
variable when space is encountered. You can also split using any
characters as we wish. Here is the code:
# Python code to illustrate split() function
with open("file.text", "r") as file:
data = file.readlines()
for line in data:
word = line.split()
print word
with open("file.text", "r") as file:
data = file.readlines()
for line in data:
word = line.split()
print word
There are also various other functions that help to manipulate the files and its contents .
###################################################################################
for this post all thanks go to www.geeksforgeeks.org
if you like than do share
here is the suggestion books which you help to learn the python perfectly
if you are the BEGINNER than this book only for you
this Python book: For Beginners: A Crash Course Guide To Learn Python in 1 Week (coding, programming, web-programming, programmer)
by
Timothy C. Needham
and Python Language
this Python book: The Complete Reference
by
Martin C. Brown
if you are the ADVANCE LEVEL than this book only for you
Comments
Post a Comment