[Tutor] Copy files
David Holland
davholla2002 at yahoo.co.uk
Mon Feb 6 14:11:28 CET 2006
Alan,
Thanks for that. I had the wrong file names, now it works, in case anyone is interested here is the code. I use it because at work I need to change different versions of sqlnet.ora :-
def compare_files(file_name1, file_name2):
x = filecmp.cmp (file_name1, file_name2)
print x
return x
def change_files(file_1, file_2, cwd):
yesorno = raw_input("Do you want them to be the same Y or N ")
#file_1 = cwd+file_1
#file_2 = cwd+file_2
yesorno = string.upper(yesorno)
if yesorno == 'Y':
try:
os.remove(file_2)
print "removed file 2"
except:
print "could not remove file"
try:
shutil.copy(file_1, file_2)
print "the copy part thinks it worked"
except:
print "it did not work"
else:
print "ok not changing anything"
def did_it_work(file_1, file_2):
#this is to debug the copy,only run if you are having problems
afterchange = compare_files(file_1,file_2 )
if afterchange == True:
print "the copy went fine"
else:
print "problem with the copy" + str(afterchange)
#main
import shutil
import string
import filecmp
import os
a = os.getcwd()
print a
file1 = "SQLNETpersonal.ORA"
file2 = "SQLNETclients.ORA"
file3 = "SQLNET.ORA"
x = compare_files(file1,file3 )
if x == False:
print file1 + " and "+ file3+ " are different"
#print "test1 and test3 are different"
change_files(file1, file3,a)
#did_it_work(file1, file3)
else:
x = compare_files(file2,file3 )
if x == False:
print file2 + " and " +file3 +" are different"
change_files(file2, file3,a)
#did_it_work(file2, file3)
print "program finished"
Alan Gauld <alan.gauld at freenet.co.uk> wrote: > The only problem is that despite the fact that as the same user,
> I can manually change these files (so I must have the right file
> permissions ?)
Can you do it from within python at the >>> prompt
Use os.getcwd() to find out where you are
Use os.chdir() to navigate
Use os.listdir() to list the folder contents
and use shutil.copy() to copy the files.
Doing it manually from inside the >>> prompt should
show up any problems. You can even try the filecomp
call from in there too... >>> is a powerful tool.
HTH,
Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
--------------------------------------------------------------------------------------------------------------------------------------
"Then you will know the truth, and the truth will set you free."
John 8:32 " As Pastor Niemöller said, first they came for Piglet and I did not speak out because I was not a Disney character."
http://www.telegraph.co.uk/opinion/main.jhtml?xml=/opinion/2005/10/04/do0402.xml
"When the facts change, I change my opinions, what do you do sir ?" John Keynes.
---------------------------------
To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060206/87bd4226/attachment.html
More information about the Tutor
mailing list