Python Practice › File Handling Programs
Python File Handling Programs with Solutions
38 file handling programs: reading, writing and appending, working line by line, counting words and characters, copying with changes, and moving the cursor. Every solution first creates its own sample file, so you can run it anywhere, including with the Try it button.
Before you start
Read the File Handling and Read/Write JSON and Read/Write CSV lesson first if the topic is new to you.
All programs
- #1Read a file in read mode
Write a Python program to read a file in reading mode.Easy - #2Overwrite a file
Write a Python program to overwrite the content of an existing file.Easy - #3Append to a file
Write a Python program to append data to an existing file.Easy - #4First three and last three lines
Write a Python program to get the first three and last three lines of a file.Easy - #5Extract emails from a file
Write a Python program to get all email addresses from a text file.Easy - #6Get a specific line
Write a Python program to get a specific line (by number) from a file.Easy - #7Copy odd lines to another file
Write a Python program to copy the odd-numbered lines of a file to a separate file.Easy - #8Read lines into a list
Write a Python program to read a file line by line into a list.Easy - #9Longest word in a file
Write a Python program to find the longest word in a file.Easy - #10Count a word in a file
Write a Python program to count how many times a word occurs in a file.Easy - #11Random line from a file
Write a Python program to read a random line from a file.Easy - #12Copy a file in lowercase
Write a Python program to copy a file’s contents to another file in lowercase.Easy - #13Copy a file in uppercase
Write a Python program to copy a file’s contents to another file in uppercase.Easy - #14Count all words in a file
Write a Python program to count all the words in a file.Easy - #15Sort lines by length
Write a Python program to sort the lines of a file by their length.Easy - #16Create A.txt to Z.txt
Write a Python program to create text files named A.txt to Z.txt.Easy - #17Odd and even length words
Write a Python program to put the words of a file into two lists by odd and even length.Easy - #18Mobile numbers from a file
Write a Python program to get all 10-digit mobile numbers from a file.Easy - #19Compare two files
Write a Python program to compare two files and show the lines that differ.Easy - #20Count lines in a file
Write a Python program to count the lines in a file.Easy - #21Size of a file
Write a Python program to get the size of a file in bytes.Easy - #22Write a tuple to a file
Write a Python program to write a tuple to a file and read it back.Easy - #23Is a file closed?
Write a Python program to check whether a file is closed.Easy - #24Characters of a file into a list
Write a Python program to extract the characters (not spaces or newlines) of a file into a list.Easy - #25Merge two files
Write a Python program to read two files and write their contents into a new file.Easy - #26Count characters in a file
Write a Python program to count the characters in a file, with and without whitespace.Easy - #27Count uppercase letters in a file
Write a Python program to count uppercase letters in a file.Easy - #28Count lowercase letters in a file
Write a Python program to count lowercase letters in a file.Easy - #29Count digits in a file
Write a Python program to count the digits in a file.Easy - #30Count special characters in a file
Write a Python program to count special characters (not letters, digits or whitespace) in a file.Easy - #31Cursor position with tell()
Write a Python program to find the current cursor position in a file.Easy - #32Move the cursor with seek()
Write a Python program to move the cursor to a specific position in a file.Easy - #33Read a file in reverse
Write a Python program to print the lines of a file in reverse order, and the whole text reversed.Easy - #34Count vowels in a file
Write a Python program to count the vowels in a file.Easy - #35Count consonants in a file
Write a Python program to count the consonants in a file.Easy - #36Drop lines containing 't'
Write a Python program to copy a file to another file, skipping lines that contain the letter “t”.Easy - #37Words shorter than 5 characters
Write a Python program to display the words of a file that have fewer than 5 characters.Easy - #38Replace spaces with underscores
Write a Python program to replace every space in a file with an underscore.Easy