Python Program to Copy the Contents of One File into Another














































Python Program to Copy the Contents of One File into Another



Description:
We have to write the program for copy the contents of one file into another file. In order to do that we first need to read the source file and store the contents of source file in any temp variable. To read the contents of file, you should prefer Python Program to Read the Contents of a File. Now we need to create new file and put the contents into that file. To create a new file, we should open the file in write mode, something like: f=open(filename, 'w').
And now put the contents into the file using 'write()' function.

For better understanding take look on Python3 Program:

import os
class File: ##File is class name
def readTextFile(self,filename):
self.filename = filename
try:
self.file = open(filename, 'r') ## opening the file in reading mode.
except:
print('{} not found'.format(self.filename)) ##if file is not found.
return False
self.content = self.file.read() ## readlines() function returns the list of all lines.
self.file.close() ## closeing the file
return self.content ## print the content of text file

def createNewFile(self,filename='input.txt',content=''):
self.filename=filename
self.content=content
self.filepath='/'.join(filename.split('/')[:-1]) ##spliting filepath from file name
if self.filepath=='': ##If not found then initilize as current directery.
self.filepath='.'
self.listOfFile=os.listdir(self.filepath)

##checking file already exist or not.
self.check=False
if self.filename in self.listOfFile:
self.message=input('{} already exist. Do you want to overwrite. Y or N:'.format(self.filename))
if self.message=='Y' or self.message=='y': ##taking user confermation for overwrite file
self.check=True
else:
self.check=False ## If user denied to overwrite file
print('Copied Failed. Try Again')
else:
self.check=True

if self.check==True:
self.file = open(filename, 'w') ## new opening file
self.file.write(self.content) ## put the content in file
self.file.close() ##closeing file
print('Copied Done')

def main():
f = File() ##creating objectof 'File' class named as f
filename1 = input('Enter Source File Name:') ##taking user input of file name
content=f.readTextFile(filename1) ## call the method of File class
if content is False:
print('copying failed...Try again')


## if source file is found
else:
filename2=input('Enter New File Name:')
##put the text in new file
f.createNewFile(filename2,content)


if __name__=='__main__':
main()


Dependencies of this output:
A text file in current directory named as 'input.txt'.
Output:
Enter Source File Name:input.txt
Enter New File Name:output.txt
Copied Done


More Articles of Dilkhush Kumar:

Name Views Likes
test1 255 4
test 328 9
Python DB2 connector get started 417 1
Python PostgreSQL 388 4
Python PostgreSQL prevent SQL injection in UPDATE 386 3
Python PostgreSQL prevent SQL injection in DELETE 431 4
Python PostgreSQL prevent SQL injection in SELECT 471 4
Python PostgreSQL join two table 368 3
Python PostgreSQL limit the no of records in a table 403 7
Python PostgreSQL update existing record 398 7
Python PostgreSQL drop table only if exists 409 9
Python PostgreSQL drop table 350 7
Python PostgreSQL delete record from the table 531 6
Python PostgreSQL sort the result in ascending order 527 7
Python PostgreSQL sort the result in descending order 600 7
Python PostgreSQL sort the result 330 6
Python PostgreSQL Order By 352 7
Python PostgreSQL wildcard select 481 6
Python PostgreSQL select with a Filter 547 6
Python PostgreSQL where example and usgae 391 6
Python PostgreSQL select using fetchone method 390 7
Python PostgreSQL select particlular column from table 348 7
Python PostgreSQL select from table 440 6
Python PostgreSQL insert record into table and get inserted ID 416 6
Python PostgreSQL multiple insert record into table 428 7
Python PostgreSQL insert record into table 438 7
Python PostgreSQL create table 379 7
Python PostgreSQL check database is exist or not 1700 16
Python PostgreSQL create database 411 19
Python PostgreSQL connector get started 403 17
Python Program to Search for a Particular Value in a Binary Tree 370 17
Python Program to Implement Bubble Sort 396 17
Python Program to Implement Queues using Stacks 424 23
Python Program to Reverse a Stack without using Recursion 444 35
Python Program to Implement a Stack using Linked List 572 31
Python Program to Implement Stack Using Two Queues 471 27
Python Program to Implement Stack using One Queue 360 23
Python Program to Implement Queue Data Structure using Linked List 457 29
Python Program to Reverse a Stack using Recursion 433 32
Python Program to Implement a Stack 451 32
Python Program to Count the Number of Occurrences of an Element in the Linked List using Recursion 430 26
Python Program to Count the Number of Occurrences of an Element in the Linked List without using Recursion 366 18
Python Program to Find the Length of the Linked List without using Recursion 338 25
Python Program to Display all the Nodes in a Linked List using Recursion 406 32
Python Program to Create a Linked List & Display the Elements in the List 421 21
Python Program to Find the Length of the Linked List using Recursion 398 20
Python Program to Search for an Element in the Linked List using Recursion 408 27
Python Program to Search for an Element in the Linked List without using Recursion 378 22
Python Program to Find Whether a Number is a Power of Two 374 24
Python Program to Implement Quicksort 410 21
Python Program to Implement Merge Sort 451 25
Python Program to Implement Queue 345 15
Python Program to Print all the Prime Numbers within a Given Range 348 18
Python Program to Count the Frequency of Words Appearing in a String Using a Dictionary 402 25
Python Program to Find the Area of a Triangle Given All Three Sides 398 27
Python Program to Read a File and Capitalize the First Letter of Every Word in the File 560 22
Python Program to Count the Occurrences of a Word in a Text File 586 24
Python Program to Copy the Contents of One File into Another 1323 14
Python Program to Append the Contents of One File to Another File 607 29
Python Program to Count the Number of Lines in a Text File 456 16
Python Program to Count the Number of Words in a Text File 710 24
Python Program to Read the Contents of a File 489 22
Python Program to Read a String from the User and Append it into a File 474 14

Comments

  • Abhay
    31-May-2019 01:28:17 PM
    Superrr