Open In App

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

Last Updated : 17 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
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:



Similar Reads

Python - Copy contents of one file to another file
Given two text files, the task is to write a Python program to copy contents of the first file into the second file. The text files which are going to be used are second.txt and first.txt: Method #1: Using File handling to read and append We will open first.txt in 'r' mode and will read the contents of first.txt. After that, we will open second.txt
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 of text is terminated with a special character call
2 min read
copy in Python (Deep Copy and Shallow Copy)
In Python, Assignment statements do not copy objects, they create bindings between a target and an object. When we use the = operator, It only creates a new variable that shares the reference of the original object. In order to create "real copies" or "clones" of these objects, we can use the copy module in Python. Syntax of Python DeepcopySyntax:
5 min read
Copy all files from one directory to another using Python
In this article, we will discuss how to copy all files from one directory to another using Python. This can be done using the shutil module. This module can be used in Python to perform operations on files and folders in a directory. Shutil package facilitates the access, movement, and removal of the files between directories. Method 1 : Using shut
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 the following data. file1.txt file2.txt Append the c
3 min read
Python program to reverse the content of a file and store it in another file
Given a text file. The task is to reverse as well as stores the content from an input file to an output file. This reversing can be performed in two types. Full reversing: In this type of reversing all the content gets reversed. Word to word reversing: In this kind of reversing the last word comes first and the first word goes to the last position.
2 min read
Python | How to copy data from one excel sheet to another
In this article, we will learn how to copy data from one excel sheet to destination excel workbook using openpyxl module in Python. For working with excel files, we require openpyxl, which is a Python library that is used for reading, writing and modifying excel (with extension xlsx/xlsm/xltx/xltm) files. It can be installed using the following com
3 min read
How Can I Make One Python File Run Another File?
In Python programming, there often arises the need to execute one Python file from within another. This could be for modularity, reusability, or simply for the sake of organization. In this article, we will explore different approaches to achieve this task, each with its advantages and use cases. Making One Python File Run Another File in PythonBel
2 min read
Copy a Python Dictionary and only Edit the Copy
Copying a dictionary in Python to preserve the original while making changes to the duplicate is a common task. Developers use methods like the `copy` module or the dictionary's `copy()` method to create an independent duplicate, allowing for isolated edits. This article explores commonly used techniques to copy a dictionary and modify only the dup
4 min read
Python Dictionary Copy Vs Deep Copy
In Python, Dictionary like other non-primitive data structures can be copied using various methods. The key distinction lies in the depth of the copy. In this article, we are going to learn about deep copy and shallow copy in Python. What is Shallow Copy in Python Dictionary?In Python, a shallow copy creates a new object that references the same un
3 min read
Article Tags :
Practice Tags :