[Tutor] Change files

David Holland davholla2002 at yahoo.co.uk
Sat Feb 11 23:09:38 CET 2006


Bruce,
 
 Thanks but is was not the solution.  It goes through all the directories but does not seem to work.
 Here is the modified code :-
 def getfiles(file1,file2,top):
     for root, dirs, files in os.walk(top):
         for name in dirs:
             print name
             for name in files:
                 if name == file1:
                     name = os.getcwd()+'/'+name
                     print "the name is" + name
                     shutil.copy(file2,name)
                     print "copied one file"
                     print os.getcwd()
 
 import os
 import shutil
 #main
 top = '/home'
 a = os.getcwd()
 filename = 'abcde'
 file1 = filename
 file2 = a+'/'+filename
 print file2
 getfiles(filename, file2,top)
 print "finished"
 
 David
 

Bruce <epost2 at gmail.com> wrote: I guess that you need to fix two things:

1 the indentaion error after for name in files:

2 specify full path for the destination arg in shutil.copy


On 2/10/06, David Holland  wrote:
> I wrote a little program that replaces all files called 'abcde' with the
> file in the directory from which you riun the program.  However it does not
> find them (there is another one).
>  What have I done wrong :-
>  #this program copies the file x to all other places in the directory.
>  #however it does not go to the right places
>  def getfiles(file1,file2,top):
>      for root, dirs, files in os.walk(top):
>          for name in dirs:
>              for name in files:
>          if name == file1:
>              shutil.copy(file2,name)
>                      print "copied one file"
>
>  import os
>  import shutil
>  #main
>  top = '/home'
>  a = os.getcwd()
>  filename = 'abcde'
>  file1 = filename
>  file2 = a+'/'+filename
>  getfiles(file1, file2,top)
>  print "finished"
>
>
>
>
>  ________________________________
> To help you stay safe and secure online, we've developed the all new Yahoo!
> Security Centre.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


		
---------------------------------
Yahoo! Photos – NEW, now offering a quality print service from just 8p a photo.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060211/105894d9/attachment.html 


More information about the Tutor mailing list