How to Replace a Specific Line in a File Using Python

Python replace specific line in file: Command Prompt output of line 3 changed with readlines and writelines

To replace a specific line in a file using Python, read every line into a list with readlines(), assign new text to that line’s index, then write the list back with writelines(). Python can’t overwrite one line in place, so the file is always rewritten: Every output below comes from a real run on Python … Read more >>

How to Call a Function from Another File in Python?

Call a Function from Another File in Python

In this tutorial, I will explain how to call a function from another file in Python. Recently someone asked about calling a function from another file, which made me research this topic and I found several ways to achieve this task. I will share my findings with suitable examples and screenshots. Call a Function from … Read more >>

How to Read an Excel File in Python

Python read Excel file: Command Prompt output of openpyxl reading sales rows from an xlsx workbook

To read an Excel file in Python, call pandas.read_excel(“file.xlsx”), which returns a DataFrame, or use openpyxl.load_workbook() when you need individual cells, formulas or formatting. Both read .xlsx files, and pandas uses openpyxl underneath: The workbook in these examples is a small 2026 sales report with a Stores sheet. I read it with Python 3.12.5, pandas … Read more >>

How to Import a Python File from the Same Directory?

Import a Python File from the Same Directory

In this tutorial, I will explain how to import a Python file from the same directory as your current script. As a developer recently I came across a scenario where I needed to import a file from the same directory. After researching I found some important methods to achieve this task, I will explain them … Read more >>

How to Get the File Size in Python (Bytes, KB, MB)

Get the file size in Python: Command Prompt output converting a file size from bytes to KB and MB with 1024 and 1000 steps

To get the file size in Python, call os.path.getsize(“file.csv”) or Path(“file.csv”).stat().st_size. Both return the size in bytes; divide by 1024 ** 2 to get megabytes. This guide shows each way to get a file size in Python, converts bytes to KB, MB and GB (and explains why Windows and disk makers disagree), adds up the … Read more >>

How to Overwrite a File in Python?

Overwrite a File in Python

Recently in a Python webinar, someone asked me how to overwrite a file in Python. After researching and experimenting I found two effective ways to achieve this task. In this tutorial, I will explain the important methods with suitable examples and screenshots. Let’s learn more about this topic today. Overwrite a File in Python Overwriting … Read more >>

How to Rename Files in Python?

Rename Files in Python

As a developer, while working on a project I came across a situation where I needed to organize a large collection of files and change the name of a single file, Python provides a simple way to accomplish this task. We’ll explore various methods, from the simplest to more advanced techniques. In this tutorial, I … Read more >>

How to Check if a File is Empty in Python?

Check if a File is Empty in Python

In this tutorial, I will explain how to check if a file is empty in Python. I recently faced this issue while building a data processing pipeline for a client in New York, where I needed to ensure that the input files were not empty before feeding them into the system. Let us explore different … Read more >>

How to Get File Name Without Extension in Python?

Get File Name Without Extension in Python

In this tutorial, I will explain how to get file name without extension in Python. As a Python programmer, we get various situations to work with files and it is important to know how to get a file name without extension. I will explain four important methods to achieve this task with examples. Get File … Read more >>

How to Write Multiple Lines to a File in Python

Write multiple lines to a file in Python: Command Prompt output showing writelines without newlines and with newlines

To write multiple lines to a file in Python, open the file with open(“file.txt”, “w”) and call f.write(“\n”.join(lines) + “\n”), or f.writelines() with each line ending in “\n”. writelines() does not add line breaks for you, which is the most common surprise. This guide compares write(), writelines(), print(file=…) and pathlib, shows how to append lines, … Read more >>