Reading a Specific Portion of a Text File python
Reading a Specific Portion of a Text File python
file_name = 'example.txt'
start_position = 10
end_position = 50
file_name = 'example.txt'
truncate_position = 20
truncate_file_at_position(file_name, truncate_position)
print("File truncated at position", truncate_position)
file_name = 'example.txt'
write_position = 30
write_data = "New content"
read_position = 15
#Below is a Python program that reads a text file and displays all the four-letter
words present in the file. Each step is explained with comments for better
understanding:python
def display_four_letter_words(file_name):
# Open the text file in read mode
with open(file_name, 'r') as file:
# Read the content of the file
content = file.read()
# Split the content into words based on whitespace
words = content.split()
print("Four-letter words in the file:")
# Iterate through each word
for word in words:
# Check if the length of the word is 4
if len(word) == 4:
# Print the four-letter word
print(word)