Python File Handling Programs, Exercises

Python File Handling Programs refers to the process of manipulating files on a computer using Python programming language. In Python, you can perform various operations on files such as reading, writing, appending, and deleting files.

To perform file handling in Python, you first need to open a file. The open() function is used to open a file. It takes two parameters – the file name (along with the path, if necessary) and the mode in which you want to open the file.

1). Python Program How to read a file in reading mode.

2). Python file program to overwrite the existing file content.

3). Python file program to append data to an existing file.

4). Python file program to get the file’s first three and last three lines.

5). Python file program to get all the email ids from a text file.

6). Python file program to get a specific line from the file.

7). Python file program to get odd lines from files and append them to separate files.

8). Python file program to read a file line by line and store it in a list.

9). Python file program to find the longest word in a file.

10). Python file program to get the count of a specific word in a file.

11). Python file program to read a random line from a file.

12). Python file program to copy the file’s contents to another file after converting it to lowercase.

13). Python file program to copy the file’s contents to another file after converting it to uppercase.

14). Python file program to count all the words from a file.

15). Python file program to sort all the lines File as per line length size.

16). Python file program to consider a text file as a DB file and store all the student information in a text file.

17). Python file program to create n number of text files with given strings.

18). Python file program to generate text files with all alphabets.  e.g. A.txt , B.txt, C.txt….. Z.txt

19). Python file program to get all odd and even length words in two lists.

20). Python file program to get all mobile numbers from a file. e.g each mobile number size should be 10.

21). Python file program to get a list of all domains from a file. e.g. .com, .au, .in

22). Python file program to compare two files.

23). Python file program to count the number of lines in a file.

24). Python file program to get the file size of a file.

25). Python file program to write a tuple to a file.

26). Python file program to check whether a file is closed or not.

27). Python file program to extract characters from a text file into a list.

28). Python file program to read the data of two of the files created and add it to a new file.

29). Python file program to count the total number of characters in a file.

30). Python file program to count the total number of Uppercase characters in a file.

31). Python file program to count the total number of Lowercase characters in a file.

32). Python file program to count the total number of digits in a file.

33). Python file program to count the total number of special characters in a file.

34). Python file program to find the cursor position in a file.

35). Python file program to move the cursor to a specific position in a file.

36). Python file program to read the content of the file in reverse order.

37). Python file program to read a file and display each word separated by @.

38). Python file program to count the total number of vowels in a file.

39). Python file program to count the total number of consonants in a file.

40). Python file program to remove all the lines that contain the character ‘t’ in a file and write it to another file.

41). Python file program to display words from a file that has less than 5 characters.

42). Python file program to replace space by an underscore in a file.

For reference here python input/output official documentation.

Leave a Comment