0% found this document useful (0 votes)
52 views4 pages

20BCS7238 - Python 3.3

This document contains the details of an experiment conducted by a student named Anukrati Sachan. The experiment involved writing Python programs to perform various file handling tasks like generating text files for each letter of the alphabet, writing letters to a file in groups of 3 per line, reading a random line from a file, counting word frequencies in a file, and copying contents from one file to another. The student has provided the code and output for 5 such programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views4 pages

20BCS7238 - Python 3.3

This document contains the details of an experiment conducted by a student named Anukrati Sachan. The experiment involved writing Python programs to perform various file handling tasks like generating text files for each letter of the alphabet, writing letters to a file in groups of 3 per line, reading a random line from a file, counting word frequencies in a file, and copying contents from one file to another. The student has provided the code and output for 5 such programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

EXPERIMENT NUMBER – 3.

Name: Anukrati Sachan UID:20BCS7238

Section: 714 A Semester-4

Date: 10/05/2022 Subject: Programming in Python Lab

Q1. Write a Python program to generate 26 text files named A.txt, B.txt, and so on up to
Z.txt
PROGRAM CODE:

import string, os
if not os.path.exists("letters"):
os.makedirs("letters")
for letter in string.ascii_uppercase:
with open(letter + ".txt", "w") as f:
f.writelines(letter)

OUTPUT:

SUBJECT NAME-Python Programming Lab SUBJECT CODE-22E-20CSP-259


Q2. Write a Python program to create a file where all letters of English alphabet are
listed by specified number of letters on each line

PROGRAM CODE:

import string
def letters_file_line(n):
with open("words1.txt", "w") as f:
alphabet = string.ascii_uppercase
letters = [alphabet[i:i + n] + "\n" for i in range(0, len(alphabet),
n)] f.writelines(letters)
letters_file_line(3)

OUTPUT:

Q3. Write a Python program to read a random line from a file.

PROGRAM CODE:

import random
def random_line(fname):
SUBJECT NAME-Python Programming Lab SUBJECT CODE-22E-20CSP-259
lines = open(fname).read().splitlines()
return random.choice(lines)
print(random_line('test.txt'))

OUTPUT:

Q4. Write a Python program to count the frequency of words in a file

PROGRAM CODE:

from collections import Counter


def word_count(fname):
with open(fname) as f:
return Counter(f.read().split())

print("Number of words in the file :",word_count("test.txt"))

OUTPUT:

Q5. Write a Python program to copy the contents of a file to another file

PROGRAM CODE:

# open both files


with open('first.txt','r') as firstfile, open('second.txt','a') as secondfile:

# read content from first file


for line in firstfile:

# append content to second


file secondfile.write(line)

OUTPUT:

SUBJECT NAME-Python Programming Lab SUBJECT CODE-22E-20CSP-259


SUBJECT NAME-Python Programming Lab SUBJECT CODE-22E-20CSP-259

You might also like