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

Report File (6)

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

Report File (6)

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

1.

) Program 1

# Define the file path


file_path = 'your_file.txt' # Replace with your file path

# Open the file and process it


with open(file_path, 'r') as file:
for line in file:
# Remove leading/trailing whitespace and split the line into
words
words = line.strip().split()
# Join the words with '#' and print the result
print('#'.join(words))

Now you can watch the stunning final episodes of the first season
of Pokémon Horizons: The Series on Netflix! It has been a thrilling
season of incredible action and adventure, with even more to
come. And with the launch of these episodes, the entire first
season is now available to watch in the US!

2.) Program 2

# Define the file path


file_path = 'your_file.txt' # Replace with your file path

# Initialize counters
vowels = "aeiouAEIOU"
vowel_count = 0
consonant_count = 0
uppercase_count = 0
lowercase_count = 0

# Open and process the file


with open(file_path, 'r') as file:
for line in file:
for char in line:
if char.isalpha(): # Check if the character is a letter
if char in vowels:
vowel_count += 1
else:
consonant_count += 1
if char.isupper():
uppercase_count += 1
elif char.islower():
lowercase_count += 1

# Display the results


print(f"Vowels: {vowel_count}")
print(f"Consonants: {consonant_count}")
print(f"Uppercase characters: {uppercase_count}")
print(f"Lowercase characters: {lowercase_count}")

3.) Program 3

# Define file paths


input_file = 'input_file.txt' # Replace with your input file path
output_file = 'output_file.txt' # Replace with your output file path

# Open the input file and output file


with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:
for line in infile:
# Check if the line contains 'a' or 'A' (case-insensitive)
if 'a' not in line.lower():
# Write the line to the output file if it doesn't contain 'a' or
'A'
outfile.write(line)

print(f"Lines without 'a' or 'A' have been written to {output_file}.")

output file

Apple
Banana
Cherry
Dates
Eggfruit
Amazing Fruit

4.) Program 4

import pickle

# File path for the binary file


binary_file = 'students.dat'

# Function to create and write data to the binary file


def create_binary_file():
with open(binary_file, 'wb') as file:
# List of students with name and roll number
students = [
{"name": "Alice", "roll_number": 101},
{"name": "Bob", "roll_number": 102},
{"name": "Charlie", "roll_number": 103},
]
# Write the list to the binary file
pickle.dump(students, file)
print("Binary file created successfully.")

# Function to search for a roll number in the binary file


def search_roll_number(roll_number):
try:
with open(binary_file, 'rb') as file:
# Load the list of students from the binary file
students = pickle.load(file)
# Search for the roll number
for student in students:
if student['roll_number'] == roll_number:
print(f"Name: {student['name']}")
return
# If not found
print("Roll number not found.")
except FileNotFoundError:
print("Binary file not found. Please create it first.")

# Create the binary file


create_binary_file()

# Search for a roll number


roll_number_to_search = int(input("Enter the roll number to
search: "))
search_roll_number(roll_number_to_search)

5.) Program 5

import pickle

# File path for the binary file


binary_file = 'students.dat'

# Function to create and write initial data to the binary file


def create_binary_file():
with open(binary_file, 'wb') as file:
# List of students with roll number, name, and marks
students = [
{"roll_number": 101, "name": "Alice", "marks": 85},
{"roll_number": 102, "name": "Bob", "marks": 90},
{"roll_number": 103, "name": "Charlie", "marks": 88},
]
# Write the list to the binary file
pickle.dump(students, file)
print("Binary file created successfully.")

# Function to update marks for a given roll number


def update_marks(roll_number, new_marks):
try:
with open(binary_file, 'rb') as file:
# Load the list of students from the binary file
students = pickle.load(file)

# Flag to check if the roll number is found


roll_found = False

# Update the marks for the matching roll number


for student in students:
if student['roll_number'] == roll_number:
student['marks'] = new_marks
roll_found = True
print(f"Updated marks for Roll Number {roll_number}:
{new_marks}")
break

if not roll_found:
print("Roll number not found.")

# Write the updated list back to the binary file


with open(binary_file, 'wb') as file:
pickle.dump(students, file)

except FileNotFoundError:
print("Binary file not found. Please create it first.")

# Create the binary file


create_binary_file()

# Input roll number and new marks


roll_number_to_update = int(input("Enter the roll number to
update marks: "))
new_marks = int(input("Enter the new marks: "))
update_marks(roll_number_to_update, new_marks)
6.) Program 6

import csv

# File path for the CSV file


csv_file = 'user_credentials.csv'

# Function to search for a password by user ID


def search_password(user_id_to_search):
try:
with open(csv_file, 'r') as file:
reader = csv.reader(file)
# Search for the user ID
for row in reader:
if row[0] == user_id_to_search:
print(f"Password for User ID '{user_id_to_search}':
{row[1]}")
return
print("User ID not found.")
except FileNotFoundError:
print("CSV file not found. Please make sure it exists.")

# Get user ID input


user_id_to_search = input("Enter the user ID to search for: ")

# Call the search_password function


search_password(user_id_to_search)

csv file
user_id,password
john_doe,12345
jane_smith,abcde

You might also like