[Tutor] Help with copying files

Bunting, Glen, IG GBunting864@Worldsavings.com
Fri, 17 May 2002 09:43:16 -0700


Hello, I am very new to Python and programming in general.  I thought that =
the best way to start learning Python would be to write a small program tha=
t does something similar to a batch file that I have already written.  Basi=
cally I want to be able to copy the same directory every night, a backup sc=
ript if you will.  Anyway, I've written what I thought would work, and it s=
tart to work and creates the root directory and a file, but when it goes to=
 create the other file, it fails.  Below is the code and the error message =
I get.  What am I doing wrong? =20

Thanks

Glen

PS

I am on a win2k machine running ActiveState Python 2.2

##Begin code

import os, shutil

def walker(arg, dirname, filenames):
	# Lists directories, files=20
	for filename in filenames:
		path =3D dirname + os.sep + filename
		print  path
		newdir =3D dirname[2:]
		os.makedirs('d:\\temp' +newdir)
		shutil.copy(path, 'd:\\temp\downloads')
		file =3D open('test1.txt', 'a')
		file.write(newdir + '\n')

os.path.walk('c:\\downloads', walker, None)


Here is the error I get:

C:\python\examples\learning>walker.py
c:\downloads\aaw.exe
c:\downloads\active ports
Traceback (most recent call last):
  File "C:\python\examples\learning\walker.py", line 14,
    os.path.walk('c:\\downloads', walker, None)
  File "C:\Python22\lib\ntpath.py", line 318, in walk
    func(arg, top, names)
  File "C:\python\examples\learning\walker.py", line 9,
    os.makedirs('d:\\temp' +newdir)
  File "C:\Python22\lib\os.py", line 203, in makedirs
    mkdir(name, mode)
OSError: [Errno 17] File exists: 'd:\\temp\\downloads'


***************************************************************************=
**
If you are not the intended recipient of this e-mail, please notify=20
the sender immediately. The contents of this e-mail do not amend=20
any existing disclosures or agreements unless expressly stated.
***************************************************************************=
**