This article will help the reader understand about the basic Python files and file handling along with some basic applications in real world. We will be using Visual Studio Code as our code editor. If you have not installed Visual Studio Code, the instructions are given in the first blog.
A file is an entity that stores information. This information may be of any type such as text, images, videos, or any music. In python, there are functions inbuilt which can be used to perform operations on files.
The open() function in python is used for opening files. This function takes two arguments, one is the filename and the other one is the mode of opening. There are many modes of opening such as read mode, write mode, and others.
Let鈥檚 explore the syntax:
# File opening in python File=open(鈥渇ilename鈥,鈥漨ode鈥)Modes of file opening:
鈥渞鈥:鈥 this is used for opening a file in read mode.
鈥渨鈥: 鈥 this is used for opening a file in write mode.
鈥渪鈥: 鈥 this is used for exclusive file creation. If the file is not present, it fails.
鈥渁鈥: 鈥 this is used when you want to append a file without truncating the file. If the file is not present, then this creates a new file.
鈥渢鈥: 鈥 this is used for opening file in text mode.
鈥渂鈥: 鈥 this is used for opening file in binary mode.
鈥+鈥: 鈥 this is used when the user wants to update a file.
Note:
The operations for binary files are as given below.Let鈥檚 open a file using above discussed methods. The code is illustrated below. As we don鈥檛 have any file, we will create a file and then open it.
x="new file opening" with open("new","w") as f: f.write(x)
In the above code, we are creating a string variable x which contains the text 鈥渘ew file opening鈥, this string variable is being written into a file 鈥渘ew鈥 using write method. We are using 鈥渨ith鈥 here as it handles closing of the file. So, we are opening a file in write format and writing the string x to the file.
Now, let鈥檚 read the same file.
x="new file opening \n writing new file" with open("new","r") as f: print(f.read())
In the above code, we are opening the file new which we wrote in the previous code and opening it in read format. Note that, we are using read() function to read the file. Let鈥檚 run and see the output.
#output New file is opening
There are three functions involved in the reading operation performed on files.
Read():This function is used when the user wants to read all the information inside the file.
x="new file opening \n writing new file" with open("new","r") as f: print(f.read())Readline():
This function is used when the user wants to read the file line by line.
x="new file opening \n writing new file" with open("new","r") as f: print(f.readline())Readlines():
This function reads all the lines but in a line by line fashion which increases its efficiency in handling memory.
x="new file opening \n writing new file" with open("new","r") as f: print(f.readlines())
As discussed above, we will be opening a file in append mode which 鈥渁+鈥 for appending it. The code is illustrated below.
x="new file opening" with open("new","a+") as f: f.write("Hello world")
Reading the file to see the appended line: x="new file opening" with open("new","r") as f: print(f.read())
Let鈥檚 explore the output:
new file openingHello world
For renaming a file, we will be using the methods present in the 鈥渙s鈥 module of python. The code is illustrated below.
import os os.rename("new.txt","example.txt")
In the above code, we are importing the 鈥渙s鈥 module and using 鈥渞ename鈥 method to rename the file we create from 鈥渘ew鈥 to 鈥渆xample鈥.
For removing files, we will be using the same module 鈥渙s鈥 which we have used for renaming the file. The example of the code is illustrated below.
import os os.remove("example.txt")
For copying the file, we will be using the same module 鈥渙s鈥 which we have used for renaming and removing a file. The example of the code is illustrated below.
import os os.system("cp example example1")
For moving the file, we will be using the same module 鈥渙s鈥 which we have used above. The example of the code is illustrated below.
import os os.system("mv source destination")
In this blog, we have covered some basics when it comes to files in Python. In the next blog post we will use all the gathered knowledge in practice.
You may also like our JavaScript Course from Beginner to Advanced.
Author: Robert Whitney
JavaScript expert and instructor who coaches IT departments. His main goal is to up-level team productivity by teaching others how to effectively cooperate while coding.
Pinterest, which made its debut on the social media scene a decade ago, never gained…
Thinking carefully on a question of how to promote a startup will allow you to…
A podcast in marketing still seems to be a little underrated. But it changes. It…
Video marketing for small business is an excellent strategy of internet marketing. The art of…
Are you wondering how to promote a startup business? We present crowdfunding platforms and websites…
How to use social media to increase sales? Well, let's start like that. Over 2.3…