0% found this document useful (0 votes)
4 views2 pages

Python Exp3

The document contains three Python programs: one for appending data to a file and displaying its contents, another for counting the number of lines, words, and characters in a file, and the last for listing files in the current directory. Each program includes code snippets and expected output. The focus is on basic file handling and directory operations in Python.

Uploaded by

ankurkumar99421
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views2 pages

Python Exp3

The document contains three Python programs: one for appending data to a file and displaying its contents, another for counting the number of lines, words, and characters in a file, and the last for listing files in the current directory. Each program includes code snippets and expected output. The focus is on basic file handling and directory operations in Python.

Uploaded by

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

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 :

You might also like