0% found this document useful (0 votes)
5 views8 pages

Python File Processing Techniques

The document outlines several Python programming tasks related to file handling, including reading a file line by line, counting characters, removing lines with specific characters, and determining file size and cursor position. Each task is accompanied by sample code and expected output. The tasks demonstrate various file operations such as counting vowels and consonants, filtering lines, and manipulating file pointers.

Uploaded by

aadharshraam12
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)
5 views8 pages

Python File Processing Techniques

The document outlines several Python programming tasks related to file handling, including reading a file line by line, counting characters, removing lines with specific characters, and determining file size and cursor position. Each task is accompanied by sample code and expected output. The tasks demonstrate various file operations such as counting vowels and consonants, filtering lines, and manipulating file pointers.

Uploaded by

aadharshraam12
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

AIM:

Read a text file line by line and display each


word separated by a #

Program Code:

with open('[Link]', 'r') as file:

for line in file:

words = [Link]().split()

print('#'.join(words))

Program Output:

1|Page
AIM:

Write a Python program to read a text file and


display the number of vowels, consonants,
uppercase letters, and lowercase letters present
in the file.

Program Code:

vowels = 'aeiouAEIOU'

vowel_count = consonant_count = uppercase_count =


lowercase_count = 0

with open('E:/file/[Link]', 'r') as file:

text = [Link]()

for char in text:

if [Link]():

if char in vowels:

vowel_count += 1

else:

consonant_count += 1

if [Link]():

uppercase_count += 1

elif [Link]():

2|Page
lowercase_count += 1

print("Vowels:", vowel_count)

print("Consonants:", consonant_count)

print("Uppercase letters:", uppercase_count)

print("Lowercase letters:", lowercase_count)

Program Output:

3|Page
AIM:

Remove all the lines that contain the character 'a' in a file and write it
to another file.

Program Code:

with open('[Link]', 'r') as infile, open('[Link]', 'w') as outfile:

for line in infile:

if 'a' not in [Link]():

[Link](line)

Program Output:

4|Page
AIM:

Write a function in Python to count the number of lines in a text file


[Link] which are starting with the alphabet 'A'.Program

Code:

with open('[Link]', 'r') as file:

for line in file:

words = [Link]().split()

print('#'.join(words))

Program Output:

5|Page
AIM:

Write a Python code to find out the size of the file in bytes, number of lines, and
number of words

Program Code:

f = open('E:/file/[Link]', 'r')

data = [Link]()

print("Size:", len([Link]()), "bytes")

print("Lines:", [Link]('\n') + 1)

print("Words:", len([Link]()))

[Link]()

Program Output:

6|Page
AIM:

Write a program to know the cursor position and print the text according to the
given specifications:

Program Code:

f = open('E:/file/[Link] ', 'r')

print("Initial position:", [Link]())

[Link](3)

print("After moving to 4th position:", [Link]())

[Link]([Link]() + 10)

print("After moving 10 more characters:", [Link]())

print("Current position:", [Link]())

print("Next 10 characters:", [Link](10))

[Link]()

7|Page
Program Output:

8|Page

You might also like