We always use print statements in Python to display the output in the console or command line terminal. However, sometimes we want to change this behavior to print to a…
In Python, when you reference a file, it needs to exist. Otherwise, Python will return a FileNotFoundError: [Errno 2] No such file or directory. In this tutorial, let’s look at…
The os module in Python comes in handy in performing any file operations such as reading a file, writing into a file etc.. The rename() method is used to rename…
There are different ways to get file size in Python. We will be using the os module and the pathlib module to check the file size. OS module in Python…
Python provides built-in functions to perform file operations, such as creating, reading, and writing files. There are mainly two types of files that Python can handle, normal text files and…
Python provides built-in functions to perform file operations, such as creating, reading, and writing files. There are mainly two types of files that Python can handle, normal text files and…
In this article, we will take a look at how to get current directory in Python. The current directory is nothing but your working directory from which your script is…
We can copy a file in Python using shutil, os, and subprocess modules. Let’s take a look at each of these modules and the functions it has with examples. Modules…
The os module is a built-in utility available in both Python 2 and 3 versions, and it provides functions to interact easily with the operating system. The os and os.path modules provide…
There are several modules available in Python to list files in a directory or folder. Some of the popular ones we can use are os, pathlib, glob, fnmatch, etc. This…
Python has several built-in modules that allow you to delete a file or remove directories. This tutorial is a step-by-step guide to remove a file or directory using 3 different…
In Python, we can extract the file extension using two approaches. Let’s take a look at each of these with examples. Python get file extension using os module splitext() function…
When you perform a file operation such as reading from a file or writing content to a file, we need to check if a file or directory exists before doing…