Here’s a Python program that reads characters from the keyboard one by one and stores them in separate files based on their case:
lower_file = 'LOWER.txt' upper_file = 'UPPER.txt' others_file = 'OTHERS.txt' try: with open(lower_file, 'w') as lower: with open(upper_file, 'w') as upper: with open(others_file, 'w') as others: while True: char = input("Enter a character (or 'q' to quit): ") if char == 'q': break elif char.islower(): lower.write(char) elif char.isupper(): upper.write(char) else: others.write(char) print("Characters saved to respective files.") except IOError: print("An error occurred while writing to the files.")
When you run the program, it will prompt you to enter a character. You can enter characters one by one. If you enter a lowercase letter, it will be stored in the file 'LOWER.txt'
. If you enter an uppercase letter, it will be stored in the file 'UPPER.txt'
. If you enter any other character, it will be stored in the file 'OTHERS.txt'
. You can stop entering characters by typing 'q'
. The program will create and write to the files 'LOWER.txt'
, 'UPPER.txt'
, and 'OTHERS.txt'
in the same directory as the script.
Please ensure that you have write permissions in the directory where the script is located.