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’s explore the syntax:
# File opening in python File=open(“filename”,”mode”)Modes of file opening:
“r”:– this is used for opening a file in read mode.
“w”: – this is used for opening a file in write mode.
“x”: – this is used for exclusive file creation. If the file is not present, it fails.
“a”: – 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.
“t”: – this is used for opening file in text mode.
“b”: – 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’s open a file using above discussed methods. The code is illustrated below. As we don’t 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 “new file opening”, this string variable is being written into a file “new” using write method. We are using “with” 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’s 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’s 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 “a+” 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’s explore the output:
new file openingHello world
For renaming a file, we will be using the methods present in the “os” module of python. The code is illustrated below.
import os os.rename("new.txt","example.txt")
In the above code, we are importing the “os” module and using “rename” method to rename the file we create from “new” to “example”.
For removing files, we will be using the same module “os” 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 “os” 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 “os” 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…