Lab Task on Files.ipynb - Colaboratory
Lab Task on Files.ipynb - Colaboratory
ipynb - Colaboratory
def read_entire_file(filename):
with open(filename, 'r') as f:
contents = f.read()
return contents
filename = 'SetH.txt'
contents = read_entire_file(filename)
print(contents)
filename = 'SetH.txt'
n = 3
lines = read_n_lines(filename, n)
for line in lines:
print(line.strip())
#Write a Python program to append text to a file and display the text.
filename = 'SetH.txt'
text = 'This is an example of appending text to a file.'
append_text_and_display(filename, text)
filename = 'SetH.txt'
n = 3
last_n_lines = read_last_n_lines(filename, n)
for line in last_n_lines:
print(line.strip())
#Write a Python program to read a file line by line and store it into a list.
def read_file_lines(filename):
with open(filename, 'r') as f:
lines = [line.strip() for line in f.readlines()]
return lines
filename = 'SetH.txt'
lines = read_file_lines(filename)
for line in lines:
print(line)
https://colab.research.google.com/drive/1SVWsUs5VwAtYdc5C9qyLhv7YktkvG-Y4#scrollTo=rGnpSxPAolsW&printMode=true 1/7
2/18/24, 8:10 PM Lab_Task_on_files.ipynb - Colaboratory
#Write a Python program to read a file line by line store it into a variable.
def read_file_line_by_line(filename):
with open(filename, 'r') as f:
lines = []
for line in f:
lines.append(line.strip())
return lines
filename = 'SetH.txt'
contents = read_file_line_by_line(filename)
#Write a Python program to read a file line by line store it into an array.
def read_file_line_by_line(filename):
with open(filename, 'r') as f:
lines = f.readlines()
return lines
filename = 'SetH.txt'
lines = read_file_line_by_line(filename)
file_contents = ''.join(lines)
print(file_contents)
def longest_word(filename):
with open(filename, 'r') as infile:
words = infile.read().split()
max_len = 0
longest_words = []
for word in words:
if len(word) > max_len:
max_len = len(word)
for word in words:
if len(word) == max_len:
longest_words.append(word)
return longest_words
filename = 'SetH.txt'
longest_words = longest_word(filename)
print(f"Longest words in {filename}: {longest_words}")
def count_lines(filename):
with open(filename, 'r') as f:
lines = f.readlines()
return len(lines)
filename = 'SetH.txt'
num_lines = count_lines(filename)
print(f'Number of lines in {filename}: {num_lines}')
https://colab.research.google.com/drive/1SVWsUs5VwAtYdc5C9qyLhv7YktkvG-Y4#scrollTo=rGnpSxPAolsW&printMode=true 2/7
2/18/24, 8:10 PM Lab_Task_on_files.ipynb - Colaboratory
#Write a Python program to get the file size of a plain file.
import os
def get_file_size(filename):
return os.path.getsize(filename)
filename = 'SetH.txt'
file_size = get_file_size(filename)
write_list_to_file('list.txt', my_list)
src_filename = 'source.txt'
dest_filename = 'destination.txt'
copy_file_contents(src_filename, dest_filename)
https://colab.research.google.com/drive/1SVWsUs5VwAtYdc5C9qyLhv7YktkvG-Y4#scrollTo=rGnpSxPAolsW&printMode=true 3/7
2/18/24, 8:10 PM Lab_Task_on_files.ipynb - Colaboratory
#Write a Python program to combine each line from first file with the corresponding line in second file.
combined_lines = []
return combined_lines
filename1 = 'file1.txt'
filename2 = 'file2.txt'
import random
def read_random_line(filename):
with open(filename, 'r') as f:
lines = f.readlines()
if lines:
return random.choice(lines).strip()
else:
return None
filename = 'SetH.txt'
random_line = read_random_line(filename)
if random_line:
print(f'Random line: "{random_line}"')
else:
print('File is empty.')
def is_file_closed(file):
try:
file.read()
except ValueError as e:
if "closed file" in str(e):
return True
return False
if is_file_closed(f):
print('File is closed.')
else:
print('File is not closed.')
https://colab.research.google.com/drive/1SVWsUs5VwAtYdc5C9qyLhv7YktkvG-Y4#scrollTo=rGnpSxPAolsW&printMode=true 4/7
2/18/24, 8:10 PM Lab_Task_on_files.ipynb - Colaboratory
#Write a Python program to remove newline characters from a file.
def remove_newline_characters(filename):
filename = 'SetH.txt'
remove_newline_characters(filename)
#Write a Python program that takes a text file as input and returns the number of words of a given text file. Note: Some words can be se
def count_words(filename):
return len(words)
num_words = count_words(filename)
#Write a Python program to extract characters from various text files and puts them into a list.
def extracts(filenames):
characters = []
return characters
characters = extracts(filenames)
print(characters)
https://colab.research.google.com/drive/1SVWsUs5VwAtYdc5C9qyLhv7YktkvG-Y4#scrollTo=rGnpSxPAolsW&printMode=true 5/7
2/18/24, 8:10 PM Lab_Task_on_files.ipynb - Colaboratory
#Write a Python program to generate 26 text files named A.txt, B.txt, and so on up to Z.txt.
import os
print(filenames)
#Write a Python program to create a file where all letters of English alphabet are listed by specified number of letters on each line.
f.write(chr(i))
if (i + 1) % num_letters_per_line == 0:
f.write('\n')
filename = 'alphabet.txt'
num_letters_per_line = 5
create_file_with_alphabets(filename, num_letters_per_line)
filename = 'SetH.txt'
n = 3
lines = read_n_lines(filename, n)
for line in lines:
print(line.strip())
filename = 'SetH.txt'
text = 'This is an example of appending text to a file.'
append_text_and_display(filename, text)
https://colab.research.google.com/drive/1SVWsUs5VwAtYdc5C9qyLhv7YktkvG-Y4#scrollTo=rGnpSxPAolsW&printMode=true 6/7
2/18/24, 8:10 PM Lab_Task_on_files.ipynb - Colaboratory
#Read numbers from a file and write even and odd numbers to separate files.
def read_numbers_and_write(filename):
with open(filename, 'r') as f:
numbers = [int(line.strip()) for line in f]
even_numbers = []
odd_numbers = []
https://colab.research.google.com/drive/1SVWsUs5VwAtYdc5C9qyLhv7YktkvG-Y4#scrollTo=rGnpSxPAolsW&printMode=true 7/7