Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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'.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Exercises, Practice Questions and Solutions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a program in Python to create a dictionary f... >>
<< Write a program in Python to encode and decode the...