0% found this document useful (0 votes)
6 views

Python TXT, Binary, CSV

Uploaded by

veeraragavankv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python TXT, Binary, CSV

Uploaded by

veeraragavankv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Here's a simple Python program that performs basic operations on a text file:

def text_file_operations(file_name):
while True:
print("\nText File Operations")
print("1. Read File")
print("2. Write to File")
print("3. Append to File")
print("4. Exit")

choice = input("Enter your choice: ")

if choice == "1":
try:
with open(file_name, 'r') as file:
print(file.read())
except FileNotFoundError:
print(f"Sorry, the file {file_name} does not exist.")

elif choice == "2":


with open(file_name, 'w') as file:
text = input("Enter text to write: ")
file.write(text)

elif choice == "3":


with open(file_name, 'a') as file:
text = input("Enter text to append: ")
file.write(text + "\n")

elif choice == "4":


break

else:
print("Invalid choice. Please choose a valid option.")

# Call the function with your file name


text_file_operations('example.txt')

This program provides a menu-driven interface to:

1. Read the contents of a text file


2. Write new text to a text file (overwriting existing content)
3. Append new text to a text file (preserving existing content)
4. Exit the program

Replace 'example.txt' with your actual file name and path.


Here's a simple Python program that performs basic operations on a binary file:

def binary_file_operations(file_name):
while True:
print("\nBinary File Operations")
print("1. Read File")
print("2. Write to File")
print("3. Append to File")
print("4. Exit")

choice = input("Enter your choice: ")

if choice == "1":
try:
with open(file_name, 'rb') as file:
print(file.read())
except FileNotFoundError:
print(f"Sorry, the file {file_name} does not exist.")

elif choice == "2":


with open(file_name, 'wb') as file:
data = input("Enter data to write (in bytes): ")
file.write(bytes(data, 'utf-8'))

elif choice == "3":


with open(file_name, 'ab') as file:
data = input("Enter data to append (in bytes): ")
file.write(bytes(data, 'utf-8'))

elif choice == "4":


break

else:
print("Invalid choice. Please choose a valid option.")

# Call the function with your file name


binary_file_operations('example.bin')

This program provides a menu-driven interface to:

1. Read the contents of a binary file


2. Write new data to a binary file (overwriting existing content)
3. Append new data to a binary file (preserving existing content)
4. Exit the program
Note:

- We open the file in binary mode ('rb' for reading, 'wb' for writing, and 'ab' for
appending) using the open function.
- When writing or appending data, we convert the input string to bytes using bytes(data,
'utf-8').

Replace 'example.bin' with your actual file name and path.

Here's a simple Python program that performs basic operations on a CSV file:

import csv

def csv_file_operations(file_name):
while True:
print("\nCSV File Operations")
print("1. Read File")
print("2. Write to File")
print("3. Append to File")
print("4. Exit")

choice = input("Enter your choice: ")

if choice == "1":
try:
with open(file_name, 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
except FileNotFoundError:
print(f"Sorry, the file {file_name} does not exist.")

elif choice == "2":


with open(file_name, 'w', newline='') as file:
writer = csv.writer(file)
data = input("Enter data to write (comma-separated): ")
writer.writerow(data.split(','))

elif choice == "3":


with open(file_name, 'a', newline='') as file:
writer = csv.writer(file)
data = input("Enter data to append (comma-separated): ")
writer.writerow(data.split(','))

elif choice == "4":


break

else:
print("Invalid choice. Please choose a valid option.")
# Call the function with your file name
csv_file_operations('example.csv')

This program provides a menu-driven interface to:

1. Read the contents of a CSV file


2. Write new data to a CSV file (overwriting existing content)
3. Append new data to a CSV file (preserving existing content)
4. Exit the program

Note:

- We use the csv module to read and write CSV files.


- When writing or appending data, we split the input string into a list using data.split(',') to create a row.

Replace 'example.csv' with your actual file name and path.

You might also like