Python File Read Write

Write a program in Python to read content from one file and write in another file.

Solution

Python has built-in open() method to open a file or create a file object. It is used for reading, writing and appending of an opened file. The open() is a factory method that creates file object.

Syntax of Python open()
open(path, mode)

Here, path is the path of file and mode parameter is an access modifier which tells the purpose of opening a file, like read, write etc.

This is the following solution to read content from one file and write in another file-

def readwrite():
    readfile = open('first.txt', 'r')
    writefile = open('second.txt', 'w')
    for line in readfile:
        line = line.upper()
        writefile.write(line)
    readfile.close()
    writefile.close()
readwrite()

The above code reads the data from the 'first.txt' and write them on 'second.txt'.





Related Articles

Python program for Prime Number
Odd Even Program in Python
Python program to convert Celsius to Fahrenheit
Python Convert XML to CSV
Python Text to Speech
Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml




Read more articles


General Knowledge



Learn Popular Language