1.
Python program to append data & display the entire file :-
with open("file.txt","a") as file:
file.write("This is new data to be appended to the file\n")
print("Data appended successfully")
with open("file.txt","r") as file:
print(file.read())
Output :
2. Python program to count no. of lines, words & characters in file :-
with open("file.txt","r") as file:
n_lines = sum(1 for line in file)
with open("file.txt","r") as file:
n_words = 0
n_chars = 0
for line in file:
n_words += len(line.split())
n_chars += len(line)
print("Number of lines: ", n_lines)
print("Number of words: ", n_words)
print("Number of character: ", n_chars)
Output :
3. Python program to display files available in current directory :-
import os
files = os.listdir()
print("Files in current directory: ")
for file in files:
print(file)
Output :