0% found this document useful (0 votes)
5 views

File handling (Python)

The document contains a series of Python programs that demonstrate various file handling operations, including reading an entire file, reading specific lines, appending text, and counting lines and word frequency. Each program is accompanied by its code and a print statement to display the output. The examples utilize functions to encapsulate the file operations for better organization and reusability.

Uploaded by

wilddreams1206
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

File handling (Python)

The document contains a series of Python programs that demonstrate various file handling operations, including reading an entire file, reading specific lines, appending text, and counting lines and word frequency. Each program is accompanied by its code and a print statement to display the output. The examples utilize functions to encapsulate the file operations for better organization and reusability.

Uploaded by

wilddreams1206
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Q1 . Write a Python program to read an entire text file.

Code :

def read_text(Myself):
with open(Myself,'r') as f:
return f.read()
print(read_text("/content/sample_data/Myself"))

Output :

Q2. Write a Python program to read first n lines of a file.

Code:

def read_n_lines(Myself, n):


with open(Myself, 'r') as file:
return [file.readline().strip() for _ in range(n)]
print(read_n_lines("/content/sample_data/Myself", 4))
Output:

Q3. Write a Python program to append text to a file and display the

text.

Code:

def append_text(Myself, text):


with open(Myself, 'a') as file:
file.write(text + '\n')
with open(Myself.strip(), 'r') as file:
return file.read()
print(append_text("/content/sample_data/Myself", "\nI have cleared all the
subjects in Sem 1."))
Output:

Q4. Write a Python program to read last n lines of a file.

Code:

from collections import deque


def read_last_n_lines(Myself, n):
with open(Myself, 'r') as file:
return list(deque(file, n))
print(read_last_n_lines("/content/sample_data/Myself", 1))
Output:
Q5. Write a Python program to read a file line by line and store it into a list.
Code :

def file_to_list(Myself):
with open(Myself, 'r') as file:
return [line.strip() for line in file]
print(file_to_list("/content/sample_data/Myself"))

Output :

Q6. Write a Python program to read a file line by line store it into a variable.

Code:

def file_to_variable(Myself):
with open(Myself, 'r') as file:
return file.read()
print(file_to_variable("/content/sample_data/Myself"))
Output:

Q7. Write a Python program to read a file line by line store it into an array.
Code:

def file_to_array(Myself):
return file_to_list(Myself)
print(file_to_array("/content/sample_data/Myself"))
Output:
Q8. Write a python program to find the longest words.

Code:

def find_longest_word(Myself):
return max(open(Myself).read().split(), key=len)
print(find_longest_word("/content/sample_data/Myself"))

Output:

Q9. Write a Python program to count the number of lines in a text file

Code:

def count_lines(Myself):
with open(Myself, 'r') as file:
return sum(1 for line in file)
print(count_lines("/content/sample_data/Myself"))
Output:

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

Code:

from collections import Counter


def word_frequency(filename):
with open(filename, 'r') as file:
return dict(Counter(file.read().split()))
print(word_frequency("/content/sample_data/Myself"))
Output:

You might also like