Friday, July 22, 2022

Python Assignment 4

 Assignment 4 : File Handling and Date-Time


Lab Assignments

SET A

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

# Program to read the entire file using read() function

file = open("python.txt", "r")

content = file.read()

print(content)

file.close()


2. Write a Python program to compute the number of characters, words and lines in a file.

file = open("sample.txt", "r")

number_of_lines = 0
number_of_words = 0
number_of_characters = 0
for line in file:
  line = line.strip("\n")
  words = line.split()
  number_of_lines += 1
  number_of_words += len(words)
  number_of_characters += len(line)
file.close()

print("lines:", number_of_lines, "words:", number_of_words, "characters:", number_of_characters)


3. Write a Python script to print the current date in following format “Sun May 29 02:26:23

IST 2017”

import time; 

ltime=time.localtime();

print(time.strftime("%a %b %d %H:%M:%S %Z %Y",ltime));  #returns the formatted time 


SET B

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

def file_read(fname):

        from itertools import islice

        with open(fname, "w") as myfile:

                myfile.write("Python Exercises\n")

                myfile.write("Java Exercises")

        txt = open(fname)

        print(txt.read())

file_read('abc.txt')


2. Write a Python program to print each line of a file in reverse order.

input_file=open('a.txt','r')

for line in input_file:

 l=len(line)

 s=' '

 while(l>=1):

 s=s+line[l-1]

 l=l-1

 print(s)

input_file.close() 


3. Write a Python program to print date, time for today and now.

import datetime

now = datetime.datetime.now()

print("Current date and time: ")

print(now.strftime('%Y-%m-%d %H:%M:%S'))

print(now.strftime('%H:%M:%S on %A, %B the %dth, %Y'))



PROGRAMS FOR PRACTICE:

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

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

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

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

5. Write a Python program to read a file line by line and store it into a list.

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

7. Write a Python program to read a file line by line store it into an array.

8. Write a python program to find the longest words.

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

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

11. Write a Python program to get the file size of a plain file.

12. Write a Python program to write a list to a file.

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

14. Write a Python program to combine each line from first file with the corresponding line in second file.

15. Write a Python program to remove newline characters from a file.

Local time: 2018-12-20 13:10:44.260462

America/New_York time: 2018-12-20 13:10:44.260462

Europe/London time: 2018-12-20 13:10:44.260462

89

16. 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 separated by a comma with no space.

17. Write a Python program to extract characters from various text files and puts them into a list.

18. Write a python program to get Current Time

19. Get Current Date and Time using Python

20. Write a python | Find yesterday’s, today’s and tomorrow’s date

21. Write a python program to convert time from 12 hour to 24 hour format

22. Write a python program to find difference between current time and given time

23. Write a python Program to Create a Lap Timer

24. Convert date string to timestamp in Python

25. Find number of times every day occurs in a Year

1 comment:

  1. Thanks for sharing these practical snippets, especially the file handling examples for computing characters, words, and lines. I remember struggling a bit with similar coding tasks back when I was juggling a tricky geography assignment help service recommendation from a mate alongside my core computer science workload. File I/O operations can sometimes throw unexpected little syntax errors if you forget to properly close or format your text streams. It’s always handy to have clean reference code like this saved for quick debugging sessions. Cheers for putting this together!

    ReplyDelete