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()
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"
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
Senthil
Bruce,
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of David Holland
Sent: Sunday, February 12, 2006 3:40 AM
To: Bruce
Cc: tutor python
Subject: Re: [Tutor] Change files
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 <[EMAIL PROTECTED]> 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 Hollandwrote:
> 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 - [email protected]
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
Yahoo! Photos – NEW, now offering a quality print service from just 8p a photo.
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
