Re: [Tutor] Change files

2006-02-12 Thread Senthil_OR



David,
in getfiles, the os.getcwd() points the main programs cwd only and 
so shutil.copy() fails.
so 
changing toname = 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 osimport shutil#maintop = r'c:\temp'a = 
os.getcwd()filename = 'some.txt'file1 = filenamefile2 = a+ os.sep 
+filenameprint file2getfiles(filename, file2,top)print 
"finished"

thanks!

--Senthil 


  
  
  From: [EMAIL PROTECTED] 
  [mailto:[EMAIL PROTECTED] On Behalf Of David 
  HollandSent: Sunday, February 12, 2006 3:40 AMTo: 
  BruceCc: tutor pythonSubject: 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 osimport shutil#maintop = 
  '/home'a = os.getcwd()filename = 'abcde'file1 = filenamefile2 
  = a+'/'+filenameprint file2getfiles(filename, file2,top)print 
  "finished"DavidBruce 
  [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.copyOn 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@python.org 
http://mail.python.org/mailman/listinfo/tutor
  
  
  Yahoo! 
  Photos  NEW, now offering a quality 
  print service from just 8p a photo.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Change files

2006-02-12 Thread David Holland
Thanks, now I understand and that works perfectly.[EMAIL PROTECTED] wrote: David, in getfiles, the os.getcwd() points the main programs cwd only and  so shutil.copy() fails. so  changing toname = os.path.join(root,name) helps us get the file in the  directory we checked against.  <d!
 iv>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 osimport shutil#maintop = r'c:\temp'a =  os.getcwd()filename = 'some.txt'file1 = filenamefile2 = a+ os.sep  +filenameprint file2getfiles(filename, file2,top)print  "finished"  thanks!  --SenthilFrom: [EMAIL PROTECTED][mailto:[EMAIL PROTECTED] On Behalf Of DavidHollandSent: Sunday, February 12, 2006 3:40 AMTo:    BruceCc: tutor pythonSubject: Re: [Tutor] Changefiles   Bruce,Thanks but is was not the solution. It goesthrough all the directories but does not seem to work.Here is the modifiedcode :-def getfiles(file1,file2,top): for root,dirs, files in os.walk(top): forname in   
 dirs:printname forname infiles:if name ==file1:name =os.getcwd()+'/'+nameprint "the name is" +nameshutil.copy(file2,name)print "copied one   
 file"print os.getcwd()import osimport shutil#maintop ='/home'a = os.getcwd()filename = 'abcde'file1 = filenamefile2= a+'/'+filenameprint file2getfiles(filename, file2,top)print"finished"DavidBruce[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.copyOn 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@python.org  http://mail.python.org/mailman/listinfo/tutor Yahoo!Photos – NEW, now offering a qualityprint service from just 8p a photo. First they came for the Danes, but I did not speak out because I am not a Dane.
		To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Change files

2006-02-11 Thread David Holland
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 [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.copyOn 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@python.org http://mail.python.org/mailman/listinfo/tutor
		Yahoo! Photos – NEW, now offering a quality print service from just 8p a photo.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Change files

2006-02-11 Thread Danny Yoo
  for root, dirs, files in os.walk(top):
  for name in dirs:
  print name
  for name in files:

This code looks suspicious.  At this point, when we say name, what do we
expect to get here?  Do you mean the directory name, or the file name?
It can't be both!  *grin*

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Change files

2006-02-10 Thread David Holland
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@python.org
http://mail.python.org/mailman/listinfo/tutor