image1 image2 image3 image3 image3 image3 image3

HELLO I'M SHASHI|WELCOME TO MY PERSONAL BLOG|I LOVE TO DO CREATIVE THINGS|I'M CYBER SECURITY STUDENT

FILE IN PYTHON I/O

                                  Types of files in Python with example



In Python, there are several types of files you can work with, including text files, binary files, and more specialized formats like JSON or CSV. Here are some common types along with examples:

1. Text Files

Text files store data in a human-readable format. You can read and write them using standard file operations.

Example: Reading and Writing a Text File

python

# Writing to a text file with open('example.txt', 'w') as file: file.write('Hello, World!\nThis is a text file.') # Reading from a text file with open('example.txt', 'r') as file: content = file.read() print(content)

2. Binary Files

Binary files store data in a format that is not human-readable, such as images or compiled code.

Example: Reading and Writing a Binary File

python

# Writing to a binary file with open('example.bin', 'wb') as file: file.write(b'\x00\x01\x02\x03') # Reading from a binary file with open('example.bin', 'rb') as file: content = file.read() print(content)


3. CSV Files

CSV (Comma-Separated Values) files are used to store tabular data.

Example: Reading and Writing CSV Files

python

import csv # Writing to a CSV file data = [['Name', 'Age'], ['Alice', 30], ['Bob', 25]] with open('example.csv', 'w', newline='') as file: writer = csv.writer(file) writer.writerows(data) # Reading from a CSV file with open('example.csv', 'r') as file: reader = csv.reader(file) for row in reader: print(row)

4. JSON Files

JSON (JavaScript Object Notation) files are used for structured data interchange.

Example: Reading and Writing JSON Files

python

import json # Writing to a JSON file data = {'name': 'Alice', 'age': 30} with open('example.json', 'w') as file: json.dump(data, file) # Reading from a JSON file with open('example.json', 'r') as file: content = json.load(file) print(content)

5. Pickle Files

Pickle files are used to serialize and deserialize Python objects.

Example: Reading and Writing Pickle Files

python

import pickle # Writing to a pickle file data = {'name': 'Alice', 'age': 30} with open('example.pkl', 'wb') as file: pickle.dump(data, file) # Reading from a pickle file with open('example.pkl', 'rb') as file: content = pickle.load(file) print(content)

6. XML Files

XML (eXtensible Markup Language) files are used to store structured data in a hierarchical format.

Example: Reading and Writing XML Files

python

import xml.etree.ElementTree as ET # Writing to an XML file root = ET.Element("people") ET.SubElement(root, "person", name="Alice", age="30") ET.SubElement(root, "person", name="Bob", age="25") tree = ET.ElementTree(root) tree.write("example.xml") # Reading from an XML file tree = ET.parse('example.xml') root = tree.getroot() for person in root.findall('person'): name = person.get('name') age = person.get('age') print(f'Name: {name}, Age: {age}')

These examples cover the most common file types you'll encounter when working with Python. Each file type has its use cases depending on the nature of the data you are handling.

Share this:

CONVERSATION

0 Comments:

Post a Comment