Change case of all characters in a .txt file using Python

If you want to change the case of all characters in a .txt file in Python then the steps will be: the first line will open the file in reading using .read() function mode located in your system.

As the file got opened the for loop will execute till the end of the file In the middle .upper() will convert the characters to upper case.

Since fo1 is opened in append mode then converted characters will be written in the target file. At the end of the file, the encounter loop will stop, and all work done.

EX:-

with open('directory_path', 'r') as f: 
    text = f.read() 
 
text = text.lower()

Convert all letters of a .txt file to uppercase characters in Python

if you want to change all characters convert lower cases to upper cases, you can use the .upper() function.

Code:-

fo = open('C:/Users/LENOVO/Desktop/CodeSpeedyProject/PythonProject1/test.txt', 'r')
for x in fo.read():
    y = x.upper()
    fo1 = open('C:/Users/LENOVO/Desktop/CodeSpeedyProject/PythonProject1/write.txt', 'a')
    fo1.write(y)

OutPut:-

Contents of test.txt:- 
Everything is good, CodeSpeedy is becoming one of the most favored companies to work in because of the intern student-centric work culture and policies.

Correct Output:- 
EVERYTHING IS GOOD, CODESPEEDY IS BECOMING ONE OF THE MOST FAVORED COMPANIES TO WORK IN BECAUSE OF THE INTERN STUDENT-CENTRIC WORK CULTURE AND POLICIES.

I am getting:-EVERYTHING IS GOOD, CODESPEEDY IS BECOMING ONE OF THE MOST FAVORED COMPANIES TO WORK IN BECAUSE OF THE INTERN STUDENT-CENTRIC WORK CULTURE AND POLICIES.

Convert all letters of a .txt file to lowercase characters in Python

If you want to change all characters convert lower cases to lower cases, you can use the .lower() function.

Code:-

fo = open('C:/Users/LENOVO/Desktop/CodeSpeedyProject/PythonProject1/test.txt', 'r')
for x in fo.read():
    y = x.lower()
    fo1 = open('C:/Users/LENOVO/Desktop/CodeSpeedyProject/PythonProject1/write.txt', 'a')
    fo1.write(y)

OutPut:-

Contents of test.txt:-
EVERYTHING IS GOOD, CODESPEEDY IS BECOMING ONE OF THE MOST FAVORED COMPANIES TO WORK IN BECAUSE OF THE INTERN STUDENT-CENTRIC WORK CULTURE AND POLICIES.
Correct Output:-
everything is good, CodeSpeedy is becoming one of the most favored companies to work in because of the intern student-centric work culture and policies.

I am getting:-everything is good, CodeSpeedy is becoming one of the most favored companies to work in because of the intern student-centric work culture and policies.

  • You cannot close the file after opening the file.

Open files with the using with statement ensure that files are correctly closed

with open('C:/Users/LENOVO/Desktop/CodeSpeedyProject/PythonProject1/test.txt', 'r') as inp:
    y = inp.read().upper()
with open('C:/Users/LENOVO/Desktop/CodeSpeedyProject/PythonProject1/write.txt', 'a') as out:
    out.write(y)

3 responses to “Change case of all characters in a .txt file using Python”

  1. Kausik Debnath says:

    Good Job. That was very crystal clear Concept. I wish I could explain everyone that easily. Thankyou Sir.

  2. Payal Agarwal says:

    The explanation is just so apt. This is so good I really liked it

  3. Thank Kumar says:

    5* out of 5* this website is really helpful. Thank You sir

Leave a Reply

Your email address will not be published. Required fields are marked *