Python allows users to handle files (read, write, save and delete files and many more). Because of Python, it is very easy for us to save multiple file formats. Python has in-built functions to save multiple file formats.
Opening a text file in Python
Opening a file refers to getting the file ready either for reading or for writing. This can be done using the
File_object = open("File_Name", "Access_Mode")
- File_Name: The name of the file that is needed to be opened.
- Access_Mode: Access modes govern the type of operations possible in the opened file.
Following are the most commonly used access modes:
- Read Only (‘r’): Open text file for reading.
- Write Only (‘w’): Open the file for writing.
- Append Only (‘a’): Open the file for writing. The data being written will be inserted at the end, after the existing data.
- Read and Write (‘r+’): Open the file for reading and writing.
Note: By default, Python assumes the access mode as read i.e (“r”)
Note: For more information, refer to Open a File in Python.
Saving a Text File in Python
After learning about opening a File in Python, let’s see the ways to save it. Opening a new file in write mode will create a file and after closing the file, the files get saved automatically. However, we can also write some text to the file. Python provides two methods for the same.
- write(): Inserts the string str1 in a single line in the text file.
- writelines(): For a list of string elements, each string is inserted in the text file. Used to insert multiple strings at a single time.
File_object.writelines(L) for L = [str1, str2, str3]
with statement in Python is used in exception handling to make the code cleaner and much more readable. It simplifies the management of common resources like file streams. Unlike the above implementations, there is no need to call
file.close() when using
with statement. The
with statement itself ensures proper acquisition and release of resources.
with open filename as file: statement(s)
Note: For more information, refer to Writing to file in Python.
Saving a CSV File in Python
CSV is a Comma Separated Values files are most widely utilized for putting tabular data. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. Python has built-in module called
csv to write and Save a CSV File.
To save a CSV File:
- First, we need to import csv library.
- Then open the file as we usually do but instead of writing content on the read_file object, we create a new object called read_writer.
- This object provides us with the writelines() method which allows us to place all the row’s data within the enter one go.
Note: For more information, refer to Writing CSV files in Python.
Saving a JSON File in Python
json. The text in JSON is done through quoted-string which contains the value in key-value mapping within
This module provides a method called
dump() which converts the Python objects into appropriate json objects.
Note: For more information, refer to Working With JSON Data in Python.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Saving a Pandas Dataframe as a CSV
- Python program to read CSV without CSV module
- Convert Text File to CSV using Python Pandas
- Create a GUI to convert CSV file into excel file using Python
- Convert JSON to CSV in Python
- Convert CSV to JSON using Python
- Convert multiple JSON files to CSV Python
- MoviePy –Saving Video File Clip
- MoviePy – Saving Video File as GIF
- Convert Text file to JSON in Python
- Python - Difference between json.dump() and json.dumps()
- How to Convert an image to NumPy array and saveit to CSV file using Python?
- Convert Text and Text File to PDF using Python
- Convert HTML table into CSV file in python
- How to save a Python Dictionary to a CSV File?
- Replacing column value of a CSV file in Python
- Saving Operated Video from a webcam using OpenCV
- Saving a Video using OpenCV
- ML - Saving a Deep Learning model in Keras
- MoviePy – Saving a Frame of Video Clip