Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more
Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c
To make everything easy: We refer CPython as Python
Files are used to store the data permanently. These are basically used to store the information. Files stores data permanently in non-volatile memory like a hard disk. Different file operations in python are:
we can open a file using open() function either in read or in write mode. The open() function takes two parameters, filename and opening mode. After calling this function, if a given filename exists then this function will return that existing file object.Syntax
Writing to a file.
>>>f=open("Hello.txt") #opens file in current directory. >>>f=open("Hello.txt","r") #opens a existing file in read mode. >>>f=open("Hello.txt","w") #opens a file in write mode, # if there's no such named file # then it will automatically create a # new file and open it.
To write something to a file, we need to open a file write mode(w) or in append mode(a). Python provides the write() method to write text or sequence of bytes (i.e, binary file) to the file. When we open a file in 'w' mode, it will create a new file with a given filename if that named file does not exist else it will open that file overwriting all the content that means contents inside the file get erased.Example
f=open('Hello.txt','w') f.write("Hello, python!") f.write("\nI love python.") f.close()
This will create a file called Hello.txt where the following text will be written to.
Hello, python! I love python.
Let us open this Hello.txt file in an append(a) mode.
with open('test.txt','a') as file: file.write("\nPython is awesome") file.write("\nPython is fun.") file.close()
This will append the sentences to the end of the file as follows:
Reading from a File.
Hello, python! I love python. Python is awesome Python is fun
To read the file in python, we should open our file in read mode(r). There is a method called read() which takes the size of the data to be read as a parameter.
If the parameter is not passed, it will read all the remaining content to the end of the file.
Let us read the content from Hello.txt that we have created.
>>>file=open('Hello.txt','r') >>>file.read(5) #reads first five characters from the file. 'Hello' >>>file.read(10) #reads next ten characters from the file. ', python!\n' >>>file.read(30) #reads next 30 characters from the file. 'I love python.\nPython is awesom' >>>file.read() #reads entire remaining file content. 'e\nPython is fun.'
In python, there is another method, called readline() which is used to read the individual lines from the file. i.e,
>>>file.readline() 'Hello python!\n' >>>file.readline() 'I love python.\n' >>>file.readline() 'Python is awesome\n'
Also, readlines() method reads the entire remaining file content to the end of the file. i.e,
Closing a file
>>>file.readlines() [', Python is fun.']
After all the file operations are done, it is a good programming practice that we should always close a file. Closing a file in python is done using the close() method.Example
This is not the proper way to close a file. This way has no surety that the file will always be closed. If any exceptions occur, the program will be terminated without closing a file. So, a better way would be using exception handling.
try: file=open('Hello.txt') finally: file.close()
Another way is to use the 'with' block. In this way, there's no need to close a file explicitly, it is done internally.
with open('Hello.txt','r') as file: # different file operations