[Tutor] Copy files

David Holland davholla2002 at yahoo.co.uk
Mon Jan 30 15:28:55 CET 2006


I wrote a small program that compares files test1 and test2 to test3.
  If the files are different  then it  copies either test1 to test3 or the reverse.
  
  The problem is that the although the shutil.copy2 in thefunction  change_files  works without error it does not copy.  Can  anyone see what is wrong :-
  
  def compare_files(file_name1, file_name2):
      x = cmp (file_name1, file_name2)
      return x
  
  def open_files(file_name):
      file_is = open(file_name,'r')
      file_conts = file_is.read()
      return file_conts
  
  def change_files(file_1, file_2):
      yesorno = raw_input("Do you want them to be the same Y or N ")
      yesorno = string.upper(yesorno)
      if yesorno == 'Y':
          try:
              shutil.copy2(file_1, file_2)
              print "copy has been done "
          except:
              print "it did not work"
     
  
  #main
  file_name1 = open_files("test1.txt")
  file_name2 = open_files("test2.txt")
  file_name3 = open_files("test3.txt")
  import shutil
  import string
  
  x = compare_files(file_name3, file_name1)
  if x != 0:
      print  "test1 and test3 are different"
      change_files("test1.txt", "text3.txt", file_name1)
  else:
      file_name1 = "test2.txt"
      print file_name1
      x = compare_files(file_name3, file_name2)
  #    print "x is " + str(x)
      if x != 0:
          print "test2 and test3 are different"
          change_files("test2.txt", "text3.txt")
  print "program finished"
  
  


--------------------------------------------------------------------------------------------------------------------------------------
 "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/20060130/6f00147e/attachment.html 


More information about the Tutor mailing list