Create a file myself.txt and Write the following content

Q.5  Create a file myself.txt and Write the following content.           
  • Hello, my name is Alex.
  • I am passionate about programming and technology
  • I enjoy learning new skills and solving challenges.
1. Open the file student_data.txt in read mode and display its contents.
2. Also append the data to the existing.
  • I believe in lifelong learning and personal growth.
  • My goal is to contribute to impactful projects in the tech world.
3.Then count number of lines, words, and characters in a file..

Solution  :- 

def create_and_write_file(): with open("myself.txt", "w") as file: file.write("Hello, my name is Alex.\n") file.write("I am passionate about programming and technology\n") file.write("I enjoy learning new skills and solving challenges.\n") def append_and_display_file(): additional_content = ( "I believe in lifelong learning and personal growth.\n" "My goal is to contribute to impactful projects in the tech world.\n" ) with open("myself.txt", "a") as file: file.write(additional_content) with open("myself.txt", "r") as file: content = file.read() print("\nContents of the file:\n") print(content) with open("myself.txt", "r") as file: lines = file.readlines() line_count = len(lines) word_count = sum(len(line.split()) for line in lines) char_count = sum(len(line) for line in lines) print("\nStatistics:") print(f"Number of lines: {line_count}") print(f"Number of words: {word_count}") print(f"Number of characters: {char_count}") create_and_write_file() append_and_display_file()

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.