Assignment 6
Assignment 6
[5]: #1
# a. center()
def center_text(text, width):
return text.center(width)
# b. repr()
def repr_text(text):
return repr(text)
# c. rjust()
def rjust_text(text, width):
return text.rjust(width)
# d. ljust()
def ljust_text(text, width):
return text.ljust(width)
# e. zfill()
def zfill_number(number, width):
return str(number).zfill(width)
# f. format()
def format_text(text, width):
return "{:<{}}".format(text, width)
# g. read()
def read_file(filename):
with open(filename, 'r') as file:
return file.read()
# h. open()
def open_file(filename, mode):
return open(filename, mode)
1
# i. tell()
def tell_position(file):
return file.tell()
# j. seek()
def seek_position(file, offset, whence):
file.seek(offset, whence)
# m. format()
def format_string(string, *args, **kwargs):
return string.format(*args, **kwargs)
# Example Usage:
if __name__ == "__main__":
# Creating and writing to a file
with open("ex.txt", "w") as file:
file.write("This is an example text file.")
# File operations
file = open_file("ex.txt", "r")
print("Current file position:", tell_position(file))
seek_position(file, 0, 0)
print("Seeking to the beginning of the file...")
print("Current file position:", tell_position(file))
2
Formatted string: Hello, world!
Current file position: 0
Seeking to the beginning of the file…
Current file position: 0
[6]: #2
import shutil
[7]: #3
import os
3
# Check if the destination file already exists
if os.path.exists(destination_filename):
print(f"The destination file '{destination_filename}' already␣
↪exists. Overwriting...")
# Example usage
source_file = "source_file.txt"
destination_file = "destination_file.bin"
copy_file_to_binary(source_file, destination_file)
[8]: #4
def count_characters(text):
vowels = 0
consonants = 0
digits = 0
special_chars = 0
def analyze_file(filename):
4
try:
with open(filename, 'r') as file:
content = file.read()
vowels, consonants, digits, special_chars =␣
↪count_characters(content)
[9]: #5
def read_file_lines(filename):
lines = []
try:
with open(filename, 'r') as file:
for line in file:
lines.append(line.strip()) # Strip newline characters and␣
↪append to the list
except FileNotFoundError:
print(f"The file '{filename}' does not exist.")
5
except Exception as e:
print("An error occurred:", e)
return lines
# Example usage
filename = "example_file.txt"
lines_array = read_file_lines(filename)
print("Contents of the file stored in an array:")
for line in lines_array:
print(line)
[10]: #6
def read_file_content(filename):
content = ""
try:
with open(filename, 'r') as file:
for line in file:
content += line # Concatenate each line to the content variable
except FileNotFoundError:
print(f"The file '{filename}' does not exist.")
except Exception as e:
print("An error occurred:", e)
return content
# Example usage
filename = "example_file.txt"
file_content = read_file_content(filename)
print("Contents of the file stored in a variable:")
print(file_content)
[11]: #7
6
print(f"Content written to '{filename}' successfully.")
except Exception as e:
print("An error occurred while writing to the file:", e)
def count_content_stats(filename):
try:
with open(filename, 'r') as file:
lines = file.readlines()
line_count = len(lines)
word_count = sum(len(line.split()) for line in lines)
space_count = sum(line.count(' ') for line in lines)
blank_line_count = sum(1 for line in lines if line.strip() == '')
↪{blank_line_count}"
7
count_content_stats("c.txt")