newbie: windows xp scripting

O

oscartheduck

I've been having trouble with the following style of script. It's
simple stuff, I know, but it's stumping me:

import os

dirfrom = 'C:\\test'
dirto = 'C:\\test1\\'

copy_command = 'copy "%s" "%s"' % (dirfrom, dirto)

if os.system(copy_command) == 0:
print "yay"
else:
print "boo"


What's going wrong is with the %s substitution part. I've tried more
complex scipts and kept running against an error where my script just
wouldn't work. So I stripped back and back and back to this point,
where I'm just trying to run a simple copy command. But for some
reason, it fails.

If I don't use the %s substitution but use the full pathnames
explicitly in the copy command, it works fine. So that's where my issue
seems to be. Help!
 
S

Steve Holden

oscartheduck said:
I've been having trouble with the following style of script. It's
simple stuff, I know, but it's stumping me:

import os

dirfrom = 'C:\\test'
dirto = 'C:\\test1\\'

copy_command = 'copy "%s" "%s"' % (dirfrom, dirto)

if os.system(copy_command) == 0:
print "yay"
else:
print "boo"


What's going wrong is with the %s substitution part. I've tried more
complex scipts and kept running against an error where my script just
wouldn't work. So I stripped back and back and back to this point,
where I'm just trying to run a simple copy command. But for some
reason, it fails.

If I don't use the %s substitution but use the full pathnames
explicitly in the copy command, it works fine. So that's where my issue
seems to be. Help!
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
Started with C:/Steve/.pythonrc ... print 'yay!'
... else:
... print 'boo!'
...
C:\Steve\t1\hello.txt
1 file(s) copied.
yay!
I don't suppose it's possible your from directory is empty?

regards
Steve
 
O

oscartheduck

It wasn't, but after seeing your success I discovered what was wrong.
My destination directory didn't exist, and for some reason windows
wasn't automatically creating it to dump the files in.

I could fix this with a nested if statement, but it "feels" like
windows should be creating this folder automatically if it doesn't
exist.

Oh well, thanks Steve! I wouldn't have performed the next step of
manunally creating the directory if you hadn't posted that the script
worked!
 
O

oscartheduck

It wasn't, but after seeing your success I discovered what was wrong.
My destination directory didn't exist, and for some reason windows
wasn't automatically creating it to dump the files in.

I could fix this with a nested if statement, but it "feels" like
windows should be creating this folder automatically if it doesn't
exist.

Oh well, thanks Steve! I wouldn't have performed the next step of
manunally creating the directory if you hadn't posted that the script
worked!
 
O

oscartheduck

For completeness' sake, this is the new script I came up with:

import os

dirfrom = 'C:\\test'
dirto = 'C:\\test1\\'
makedir = 'mkdir "%s"' % dirto
copy_command = 'copy "%s" "%s"' % (dirfrom, dirto)

if os.system(copy_command) == 0:
print "yay"
else:
if os.system(makedir) == 0:
if os.system(copy_command) == 0:
print 'yay!'
else:
print 'WTF mate?'


Is there a more efficient way of doing this?
 
L

Larry Bates

oscartheduck said:
For completeness' sake, this is the new script I came up with:

import os

dirfrom = 'C:\\test'
dirto = 'C:\\test1\\'
makedir = 'mkdir "%s"' % dirto
copy_command = 'copy "%s" "%s"' % (dirfrom, dirto)

if os.system(copy_command) == 0:
print "yay"
else:
if os.system(makedir) == 0:
if os.system(copy_command) == 0:
print 'yay!'
else:
print 'WTF mate?'


Is there a more efficient way of doing this?
Suggested changes:

import os
import glob
import shutil

dirfrom = 'C:\\test'
dirto = 'C:\\test1'

if not os.path.exists(dirto): os.makedirs(dirto)
for src in glob.glob(os.path.join(dirfrom, "*.*")):
dst=os.path.join(dirto, os.path.basename(src))
#print "Copying %s->%s" % (src, dst)
shutil.copyfile(src, dst)


-Larry Bates
 
T

Tim Roberts

oscartheduck said:
It wasn't, but after seeing your success I discovered what was wrong.
My destination directory didn't exist, and for some reason windows
wasn't automatically creating it to dump the files in.

Right. The "copy" command never creates directories. It will copy each
individual file, in turn, to a file with whatever the last name in the list
is. Not terribly useful, but it is a behavior inherited from DOS 1.

On the other hand, the "xcopy" command will do it:

xcopy /i C:\DIR1 C:\DIR2

That will create DIR2 if it does not already exist.
I could fix this with a nested if statement, but it "feels" like
windows should be creating this folder automatically if it doesn't
exist.

What "feels" right is rarely a good reference for a command's behavior,
especially on Windows.
 
T

Tim Golden

Tim said:
Right. The "copy" command never creates directories. It will copy each
individual file, in turn, to a file with whatever the last name in the list
is. Not terribly useful, but it is a behavior inherited from DOS 1.

On the other hand, the "xcopy" command will do it:

xcopy /i C:\DIR1 C:\DIR2

That will create DIR2 if it does not already exist.

I've done a v. simple comparison of a few techniques
for copying things on Win32. Might be of some use:

http://timgolden.me.uk/python/win32_how_do_i/copy-a-file.html

TJG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top