0% found this document useful (0 votes)
3 views3 pages

Expt 5

The document contains three main functions for file operations in Python. The first function appends data to a file and displays its contents, the second counts lines, words, and characters in a specified file, and the third lists all files in the current directory. Each function is encapsulated in a main function that is executed when the script is run.

Uploaded by

shanne724
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Expt 5

The document contains three main functions for file operations in Python. The first function appends data to a file and displays its contents, the second counts lines, words, and characters in a specified file, and the third lists all files in the current directory. Each function is encapsulated in a main function that is executed when the script is run.

Uploaded by

shanne724
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

5.

1
# Function to append data to a file
def append_to_file(filename, data):
with open(filename, 'a') as file: # Open the file in append mode
file.write(data + '\n') # Write the data followed by a newline

# Function to display the contents of a file


def display_file_contents(filename):
with open(filename, 'r') as file: # Open the file in read mode
contents = file.read() # Read the entire contents of the file
print(contents) # Print the contents

# Main function
def main():
filename = 'example.txt' # Specify the filename

# Get data from the user


data = input("Enter the data you want to append to the file: ")

# Append data to the file


append_to_file(filename, data)

# Display the contents of the file


print("\nContents of the file after appending:")
display_file_contents(filename)

# Run the main function


if __name__ == "__main__":
main()

OUTPUT:
5.2
# Function to count lines, words, and characters in a file
def count_file_contents(filename):
try:
with open(filename, 'r') as file: # Open the file in read mode
lines = file.readlines() # Read all lines in the file
line_count = len(lines) # Count the number of lines
word_count = 0 # Initialize word count
char_count = 0 # Initialize character count
for line in lines:
words = line.split() # Split the line into words
word_count += len(words) # Count words in the line
char_count += len(line) # Count characters in the line

return line_count, word_count, char_count # Return counts


except FileNotFoundError:
print(f"The file '{filename}' does not exist.")
return None, None, None

# Main function
def main():
filename = 'example.txt' # Specify the filename
# Count lines, words, and characters
line_count, word_count, char_count = count_file_contents(filename)

# Display the results


if line_count is not None:
print(f"Number of lines: {line_count}")
print(f"Number of words: {word_count}")
print(f"Number of characters: {char_count}")

# Run the main function


if __name__ == "__main__":
main()
OUTPUT:
5.3
import os # Import the os module to interact with the operating system

# Function to list files in the current directory


def list_files_in_directory():
# Get the current working directory
current_directory = os.getcwd()
print(f"Current Directory: {current_directory}\n")

# List all files and directories in the current directory


files = os.listdir(current_directory)

# Filter out only files (excluding directories)


files = [f for f in files if os.path.isfile(os.path.join(current_directory, f))]

# Check if there are any files and display them


if files:
print("Files in the current directory:")
for file in files:
print(file)
else:
print("No files found in the current directory.")

# Main function
def main():
list_files_in_directory() # Call the function to list files

# Run the main function


if __name__ == "__main__":
main()
OUTPUT:

You might also like