[Tutor] Change files
Senthil_OR at Dell.com
Senthil_OR at Dell.com
Sun Feb 12 13:41:47 CET 2006
David,
in getfiles, the os.getcwd() points the main programs cwd only and so
shutil.copy() fails.
so changing to name = os.path.join(root,name) helps us get the file in
the directory we checked against.
def getfiles(file1,file2,top):
for root, dirs, files in os.walk(top):
for dirname in dirs:
print dirname
for name in files:
if name == file1:
name = os.path.join(root,name)
print "the name is" + name
shutil.copy(file2,name)
print "copied one file"
print os.getcwd()
import os
import shutil
#main
top = r'c:\temp'
a = os.getcwd()
filename = 'some.txt'
file1 = filename
file2 = a+ os.sep +filename
print file2
getfiles(filename, file2,top)
print "finished"
thanks!
--
Senthil
_____
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org]
On Behalf Of David Holland
Sent: Sunday, February 12, 2006 3:40 AM
To: Bruce
Cc: tutor python
Subject: Re: [Tutor] Change files
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
<http://us.rd.yahoo.com/mail/uk/taglines/gmail_com/photos/*http://uk.pho
tos.yahoo.com/> - NEW, now offering a quality print service
<http://us.rd.yahoo.com/mail/uk/taglines/gmail_com/photos/*http://uk.pho
tos.yahoo.com/> from just 8p a photo.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060212/e5b45817/attachment.htm
More information about the Tutor
mailing list