Experience: is what you get soon after you need it.

Experience: is what you get soon after you need it.

Rasul Allah (sal Allahu alaihi wa sallam) said: "Restore the trusts of those who trust you, and deal not falsely with him who deals falsely with you." [Abu Dawud, Tirmidhi]

Search This Blog

Monday, October 19, 2015

Copying contents from one file into another using python:

Copying contents from one file into another using python:

shaiks@MAC$vi 8files.py

from sys import argv
from os.path import exists


script, old, new =argv

while True:
        if exists(new):
           print "file already exists\t",new
           if (raw_input("Continue with wipe out: y|n:\t")) == 'Y':
                new1=(new,'w')
                break
           else:
                print "You entered No.....appending data to the %s file\t" % new
                #exit(1)
                new1=open(new,'a')
           break
        else:
         print "Creating new file....%s\t:" % new
         new1=open(new,'w')
         break

old1 = open(old).read()
print "Old file contents are%s\n" % old1

#new1.close()
print "Appenind data to the file\t",new
new1 =  new.write(old1)

print "Closing files that were opened..."
#old.close()
#new.close()


#Contents of ps.out
shaiks@MAC$cat ps.out

Write these new lins into the ls file

line3 left blank
Write these new lins into the ls file
Write these new lins into the ls file
line below me left blank


#Contents of ls.out
shaiks@MAC$cat ls.out

I already have this line in this file

##Executing the  script

shaiks@MAC$python 8files.py ps.out ls.out
file already exists    ls.out
Continue with wipe out: y|n:    n
You entered No.....appending data to the ls.out file   
Old file contents are
Write these new lins into the ls file

line3 left blank
Write these new lins into the ls file
Write these new lins into the ls file
line below me left blank



Appending data to the file    ls.out
Closing files that were opened...

shaiks@MAC$cat ls.out

I already have this line in this file

Write these new lins into the ls file

line3 left blank
Write these new lins into the ls file
Write these new lins into the ls file
line below me left blank

 

No comments: