Python File
- 08 March
Read File without Newline in Python
Read File without NewLine in Python In Python, there are multiple ways to read a file without new lines, but before diving into those methods, let’s look at the .txt file we will use for this article. [crayon-6768395faf812268884966/] You can follow along using the file.txt or your own file. Use read() and replace() Methods To […]
- 13 February
Count Files in Directory in Python
Using os Module We can use the listdir(), scandir(), and walk() methods of the os module to count files in the current directory, including/excluding sub-directories. Before diving into the details, let’s look at the directory structure we will use for this article; you may use your own. E:\Test – contains two text files (File1.txt and […]
- 21 December
Save Object to File in Python
1. Introduction In Python, one often encounters the need to save complex objects, like instances of a class, to a file for purposes such as data persistence. Let’s take a Student class as an example, which includes attributes like name, email, age, city, courses, and address. The goal is to efficiently serialize this object into […]
- 18 December
Write Binary File in Python
Use bytearray() Function To write a binary file in Python: Use the bytearray() function to convert the list of bytes to a bytearray type object. Use a with clause with open() the method in write binary mode(wb) Use write() method to write the byte_array to the file. [crayon-6768395faff12551053010/] [crayon-6768395faff15996472018/] We created a byte_list using a […]
- 26 November
Remove Empty Lines from Text File in Python
Use isspace() Method To remove empty lines from a Text File in Python: Open a file in read and write mode(r+). Use for loop for iterate over each line in the file. Use isspace() method to check if line is empty. if it is not empty, add it to result. Use seek(0) to move cursor […]
- 16 November
Create Temp File in Python
Sometimes applications require storage and processing of temporary data. For such purposes, Python provides the tempfile library that can be used to create such files and directories. How to Create Temp File in Python This tutorial will demonstrate how to create temp file in Python. Using the tempfile.NamedTemporaryFile object The NamedTemporaryFile constructor is used to […]
- 02 October
Get Temp Directory in Python
Sometimes we can encounter scenarios where we wish to store temporary data in Python. This can include situations of creating temporary files and directories. In this tutorial, we will discuss how to get temp directory in Python. Get Temp Directory in Python The tempfile module is used in Python to work with and create temporary […]
- 19 May
Remove Extension From Filename in Python
While programming in python, we often have to deal with file names. In this article, we will discuss how we can Remove Extension From Filename in python. How to Remove Extension From Filename in Python? To Remove Extension From Filename in python, we can use the functions provided in the os module or the pathlib […]
- 19 May
Get Directory Name From File Path in Python
File operations are a fundamental part of python application development. In this article, we will discuss how we can get the directory name from the file path in python. Later, we will also discuss how we can remove filename from the file path in python. How to Get directory name from file path in python? […]