Thanks, now I understand and that works perfectly.<br><br><b><i>Senthil_OR@Dell.com</i></b> wrote:<blockquote class="replbq" style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"> <meta http-equiv="Content-Type" content="text/html; charset=us-ascii"> <meta content="MSHTML 6.00.2800.1528" name="GENERATOR"> <div><span class="241373612-12022006"><font color="#0000ff" face="Verdana" size="2">David,</font></span></div> <div><span class="241373612-12022006"><font color="#0000ff" face="Verdana" size="2"> in getfiles, the os.getcwd() points the main programs cwd only and so shutil.copy() fails.</font></span></div> <div><span class="241373612-12022006"><font color="#0000ff" face="Verdana" size="2">so changing to name = os.path.join(root,name) helps us get the file in the directory we checked against.</font></span></div> <div><span class="241373612-12022006"><font color="#0000ff" face="Verdana" size="2"></font></span> </div> <div><span
class="241373612-12022006"><font color="#0000ff" face="Verdana" size="2">def getfiles(file1,file2,top):<br> for root, dirs, files in os.walk(top):<br> for dirname in dirs:<br> print dirname<br> for name in files:<br> if name == file1:<br> name = os.path.join(root,name)<br> print "the name is" + name<br>
shutil.copy(file2,name)<br> print "copied one file"<br> print os.getcwd()</font></span></div> <div><font color="#0000ff" face="Verdana" size="2"></font> </div> <div><span class="241373612-12022006"><font color="#0000ff" face="Verdana" size="2">import os<br>import shutil<br>#main<br>top = r'c:\temp'<br>a = os.getcwd()<br>filename = 'some.txt'<br>file1 = filename<br>file2 = a+ os.sep +filename<br>print file2<br>getfiles(filename, file2,top)<br>print "finished"<br></font></span></div> <div><span class="241373612-12022006"><font color="#0000ff" face="Verdana" size="2"></font></span> </div> <div><span class="241373612-12022006"><font color="#0000ff" face="Verdana" size="2">thanks!</font></span></div> <div><span class="241373612-12022006"><font
color="#0000ff" face="Verdana" size="2"></font></span> </div> <div><font size="2">--<br>Senthil<br> </font> </div> <div> </div><br> <blockquote style="margin-right: 0px;"> <div class="OutlookMessageHeader" align="left" dir="ltr" lang="en-us"> <hr tabindex="-1"> <font face="Tahoma" size="2"><b>From:</b> tutor-bounces@python.org [mailto:tutor-bounces@python.org] <b>On Behalf Of </b>David Holland<br><b>Sent:</b> Sunday, February 12, 2006 3:40 AM<br><b>To:</b> Bruce<br><b>Cc:</b> tutor python<br><b>Subject:</b> Re: [Tutor] Change files<br></font><br></div> <div></div>Bruce,<br><br>Thanks but is was not the solution. It goes through all the directories but does not seem to work.<br>Here is the modified code :-<br>def getfiles(file1,file2,top):<br> for root, dirs, files in os.walk(top):<br> for name in
dirs:<br> print name<br> for name in files:<br> if name == file1:<br> name = os.getcwd()+'/'+name<br> print "the name is" + name<br> shutil.copy(file2,name)<br> print "copied one
file"<br> print os.getcwd()<br><br>import os<br>import shutil<br>#main<br>top = '/home'<br>a = os.getcwd()<br>filename = 'abcde'<br>file1 = filename<br>file2 = a+'/'+filename<br>print file2<br>getfiles(filename, file2,top)<br>print "finished"<br><br>David<br><br><br><b><i>Bruce <epost2@gmail.com></i></b> wrote: <blockquote class="replbq" style="border-left: 2px solid rgb(16, 16, 255); padding-left: 5px; margin-left: 5px;">I guess that you need to fix two things:<br><br>1 the indentaion error after for name in files:<br><br>2 specify full path for the destination arg in shutil.copy<br><br><br>On 2/10/06, David Holland <DAVHOLLA2002 @yahoo.co.uk="">wrote:<br>> I wrote a little program that replaces all files called 'abcde' with the<br>> file in the directory from which you riun the program. However it does not<br>>
find them (there is another one).<br>> What have I done wrong :-<br>> #this program copies the file x to all other places in the directory.<br>> #however it does not go to the right places<br>> def getfiles(file1,file2,top):<br>> for root, dirs, files in os.walk(top):<br>> for name in dirs:<br>> for name in files:<br>> if name == file1:<br>> shutil.copy(file2,name)<br>> print "copied one file"<br>><br>> import os<br>> import shutil<br>> #main<br>> top = '/home'<br>> a = os.getcwd()<br>> filename = 'abcde'<br>> file1 = filename<br>> file2 = a+'/'+filename<br>> getfiles(file1, file2,top)<br>> print "finished"<br>><br>><br>><br>><br>> ________________________________<br>> To help you stay safe and secure online, we've developed the all new Yahoo!<br>> Security Centre.<br>><br>><br>>
_______________________________________________<br>> Tutor maillist - Tutor@python.org<br>> http://mail.python.org/mailman/listinfo/tutor<br>><br>><br>><br></DAVHOLLA2002></blockquote><br> <div> </div><hr size="1"> <font face="Arial" size="2"><a href="http://us.rd.yahoo.com/mail/uk/taglines/gmail_com/photos/*http://uk.photos.yahoo.com/"><b>Yahoo! Photos</b></a> – <font color="red">NEW</font>, now offering a <a href="http://us.rd.yahoo.com/mail/uk/taglines/gmail_com/photos/*http://uk.photos.yahoo.com/">quality print service</a> from just 8p a photo.</font></blockquote></blockquote><br><BR><BR>------------------------------------------------------------------------------------------------------------------------------------<br> First they came for the Danes, but I did not speak out because I am not a Dane.<br><p>
                <hr size=1><font face="Arial" size="2">To help you stay safe and secure online, we've developed the all new <a href="http://us.rd.yahoo.com/mail/uk/taglines/gmail/security_centre/*http://uk.security.yahoo.com/"><b>Yahoo! Security Centre</b></a>.</font>