brel-logo-white brel-logo-white
  • Travel
  • Case Study
  • How to
  • Technology
  • More
    • Sports
    • Health
    • Articles
    • Review
Notification
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
BreldigitalBreldigital
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
Search
  • Quick Access
    • Home
    • History
    • My Saves
    • My Interests
    • My Feed
  • Categories
    • Travel
    • Sports
    • Health

Top Stories

Explore the latest updated news!

Which statement about immersion suits is true?

Choose the correct synonyms

Write sentences with these words

Stay Connected

Find us on socials
248.1k Followers Like
61.1k Followers Follow
165k Subscribers Subscribe
Breldigital > Blog > ds > How to append to a file in python
ds

How to append to a file in python

By John Published November 10, 2022
Share

This is a Python Program to append the contents of one file to another file.

Problem Description
The program takes the name of the file to be read from and the file to append into from the user and appends the contents of one file to another.

Problem Solution
1. Take the name of the file to be read from and the file to append into from the user.
2. Open the file to be read and read the content of the file and store it in a variable.
3. Open the file to append the data to and write the data stored in the variable into the file.
4. Close both the files.
5. Exit.

Program/Source Code
Here is source code of the Python Program to append the contents of one file to another file. The program output is also shown below.

name1 = input(“Enter file to be read from: “)
name2 = input(“Enter file to be appended to: “)
fin = open(name1, “r”)
data2 = fin.read()
fin.close()
fout = open(name2, “a”)
fout.write(data2)
fout.close()

Runtime Test Cases

Case 1:

Output:
Enter file to be read from: test.txt
Enter file to be appended to: test1.txt

Contents of file test.txt:
Appending!!

Contents of file test1.txt (before appending):
Original

Contents of file test1.txt (after appending):
Original Appending!!

Case 2:
Enter file to be read from: out.txt
Enter file to be appended to: out1.txt

Contents of file test.txt:
world
Contents of file test1.txt (before appending):
Hello

Contents of file test1.txt (after appending):
Hello world

TAGGED: How to append to a file in python
John November 10, 2022 November 10, 2022

Search

brel-logo-white brel-logo-white
Explore a wide range of brands and categories with our comprehensive coverage, and stay up-to-date with the latest news and trends by subscribing to our updates.
Categories:
  • Entertainment
  • Travel
  • Sport
  • Contact Us

Quick Links

  • My Feed
  • My Interests
  • History
  • My Saves

About US

  • Adverts
  • Our Jobs
  • Term of Use

© 2023 Brel Digital All Rights Reserved. All logos and images used on this website are registered trademarks of their respective companies

Welcome Back!

Sign in to your account

Lost your password?