Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to move all files from one directory to another using Python ?
Next article icon

Python – Copy all the content of one file to another file in uppercase

Last Updated : 17 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to write a Python program to copy all the content of one file to another file in uppercase. In order to solve this problem, let’s see the definition of some important functions which will be used:

  • open() – It is used to open a file in various modes like reading, write, append, both read and write.
  • write() – It is used to write a specified text to a file.
  • upper() – It is used to convert all the lowercase letters to uppercase letters of a string and finally returns it.

Method 1: Using file handling to read and write 

In this method, we would be using ‘r’ mode(to read the file) in the first file and ‘w’ mode(to write in a file) in the second file. Finally, we will write the content of the first file into the second file by using the write method. We will use the upper method there to capitalize the content while writing the content into the second file.

Python

# To open the first file in read mode
f1 = open("sample file 1.txt", "r")
  
# To open the second file in write mode
f2 = open("sample file 2.txt", "w")
  
# For loop to traverse through the file
for line in f1:
  
    # Writing the content of the first
    # file to the second file
      
    # Using upper() function to
    # capitalize the letters
    f2.write(line.upper())  
                      
                       

Output:

Method 2: Using file handling to read and append

In this method, we would be using ‘r’ mode(to read the file) in the first file and ‘a’ mode(to append the content) in the second file. Finally, we will write the content of the first file into the second file by using the write method. We will use the upper method there to capitalize the content while writing the content into the second file.

Python

# To open the first file in read mode
f1 = open("sample file 1.txt", "r")
  
# To open the second file in append mode
f2 = open("sample file 2.txt", "a")
  
# For loop to traverse through the file
for line in f1:
  
    # Writing the content of the first
    # file to the second file
      
    # Using upper() function
    # to capitalize the letters
    f2.write(line.upper())  
                      
                       

Output:



Next Article
How to move all files from one directory to another using Python ?
author
kunalmali
Improve
Article Tags :
  • Python
  • python-file-handling
Practice Tags :
  • python

Similar Reads

  • Copy all files from one directory to another using Python
    Copying files from one directory to another involves creating duplicates of files and transferring them from one folder to another. This is helpful when organizing files, backing them up, or moving them to different locations on your computer. Let’s explore various methods to do this efficiently. Us
    2 min read
  • Python - Append content of one text file to another
    Having two file names entered by users, the task is to append the content of the second file to the content of the first file with Python. Append the content of one text file to anotherUsing file objectUsing shutil moduleUsing fileinput moduleSuppose the text files file1.txt and file2.txt contain th
    3 min read
  • How to move all files from one directory to another using Python ?
    In this article, we will see how to move all files from one directory to another directory using Python.  In our day-to-day computer usage we generally copy or move files from one folder to other, now let's see how to move a file in Python: This can be done in two ways:Using os module.Using shutil m
    2 min read
  • Read content from one file and write it into another file
    Prerequisite: Reading and Writing to text files in Python Python provides inbuilt functions for creating, writing, and reading files. Two types of files can be handled in python, normal text files and binary files (written in binary language,0s, and 1s). Text files: In this type of file, Each line o
    2 min read
  • Change case of all characters in a .txt file using Python
    In this article, we will learn how to change the case of all characters present in a text file using Python. We will use Python methods Python upper() to change all characters to upper case, and Python lower() to change all characters to lower case. For example, If we have text file data.txt as show
    3 min read
  • How to run multiple Python file in a folder one after another?
    In this article, we will discuss how to run multiple python files in a folder one after another. There can be many ways to this task, here, we will discuss a few of them. For doing this program, we have to create some python files in one folder and give some names to that folder.  The content inside
    3 min read
  • How to create a duplicate file of an existing file using Python?
    In this article, we will discuss how to create a duplicate of the existing file in Python. Below are the source and destination folders, before creating the duplicate file in the destination folder. After a duplicate file has been created in the destination folder, it looks like the image below. For
    5 min read
  • Find all the Files in a Directory with .txt Extension in Python
    Directory traversal is a common operation performed by file locator in Operating Systems. The operating system offers elaborate methods for streamlining the search process and the choice to search for a selected filename/extension. This article will teach you how to find all the files in a directory
    5 min read
  • How to open and close a file in Python
    There might arise a situation where one needs to interact with external files with Python. Python provides inbuilt functions for creating, writing, and reading files. In this article, we will be discussing how to open an external file and close the same using Python. Opening a file in Python There a
    4 min read
  • Detect Encoding of a Text file with Python
    Python provides a straightforward way to determine the encoding of a text file, essential for the proper handling of diverse character sets. The chardet library is a popular choice for automatic character encoding detection. By analyzing the statistical distribution of byte values, it accurately ide
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences