Re: [Tutor] Convert my .bat and .vbs to .py

2007-02-15 Thread Mark Bystry
Ahh. Great tip Eric. Thank-you.

Mark


Eric Walstad wrote the following on 2/15/2007 2:58 PM:
> Hey Mark,
> Mark Bystry wrote:
>> sh.copy('C:\testing_it.txt', 'D:\')
> 
> Also have a look at the os.path module.  Because you want these scripts
> to work on both Linux and Windows, using os.path will let you avoid
> writing platform specific code to handle your slashes.
> 
> For example:
> import os
> path_parts_list = ['C:', 'testing_it.txt']
> 
> Now, os.path.join will join those path parts together with whatever is
> appropriate for the OS you are running on:
> 
> os.path.join(path_parts_list)
> 
> This should give you:
> 'c:\testing_it.txt' on Windows and
> 'c:/testing_it.txt' on Linux (which doesn't make sense, but demonstrates
> how os.path can handle your os-specific slashes for you).
> 
> There are lots of helpful path related built-ins:
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Convert my .bat and .vbs to .py

2007-02-15 Thread Eric Walstad
Hey Mark,
Mark Bystry wrote:
> sh.copy('C:\testing_it.txt', 'D:\')

Also have a look at the os.path module.  Because you want these scripts
to work on both Linux and Windows, using os.path will let you avoid
writing platform specific code to handle your slashes.

For example:
import os
path_parts_list = ['C:', 'testing_it.txt']

Now, os.path.join will join those path parts together with whatever is
appropriate for the OS you are running on:

os.path.join(path_parts_list)

This should give you:
'c:\testing_it.txt' on Windows and
'c:/testing_it.txt' on Linux (which doesn't make sense, but demonstrates
how os.path can handle your os-specific slashes for you).

There are lots of helpful path related built-ins:

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


Re: [Tutor] Convert my .bat and .vbs to .py

2007-02-15 Thread Mark Bystry
Jeez! I'm retarded. It works like a charm now.

Thanks for that Rob

Mark

Rob Andrews wrote the following on 2/15/2007 2:29 PM:
> Ahh, yes. Your main trouble seems to be the use of \ instead of / in
> 'C:\testing_it.txt'
> 
> Windows uses \, whereas python uses /. So you can replace 'c:\'
> with your choice of the following:
> 
> 'c:/' (replacing \ with /)
> r'c:\' (the 'r' before the string tells python it's a raw string)
> 'c:\\' (the first \ telling python to take the second \ at face value)
> 
> -Rob A.
> 
> On 2/15/07, Mark Bystry <[EMAIL PROTECTED]> wrote:
>> Well, immediately I am having problems. Be patient with me.
>>
>> This what I have...
>>
>> copy_file.py
>> 
>>
>> import os
>> import shutil as sh
>>
>> sh.copy('C:\testing_it.txt', 'D:\')
>> raw_input("Done!")
>>
>> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Convert my .bat and .vbs to .py

2007-02-15 Thread Kent Johnson
Mark Bystry wrote:
> Well, immediately I am having problems. Be patient with me.

We will be, you're doing great so far :-)
> 
> This what I have...
> 
> copy_file.py
> 
> 
> import os
> import shutil as sh
> 
> sh.copy('C:\testing_it.txt', 'D:\')

Backslash is an escape character in Python string constants. \t actually 
inserts a tab character in the string.

You have several choices:

'C:/testing_it.txt' - forward slashes work fine too
'C:\\testing_it.txt' - double the \ to make it a literal.
r'C:\testing_it.txt' - 'r' prefix makes it a 'raw' string, backslashes 
are (mostly) treated as literals rather than escapes.

Kent

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


Re: [Tutor] Convert my .bat and .vbs to .py

2007-02-15 Thread Rob Andrews
Ahh, yes. Your main trouble seems to be the use of \ instead of / in
'C:\testing_it.txt'

Windows uses \, whereas python uses /. So you can replace 'c:\'
with your choice of the following:

'c:/' (replacing \ with /)
r'c:\' (the 'r' before the string tells python it's a raw string)
'c:\\' (the first \ telling python to take the second \ at face value)

-Rob A.

On 2/15/07, Mark Bystry <[EMAIL PROTECTED]> wrote:
> Well, immediately I am having problems. Be patient with me.
>
> This what I have...
>
> copy_file.py
> 
>
> import os
> import shutil as sh
>
> sh.copy('C:\testing_it.txt', 'D:\')
> raw_input("Done!")
>
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Convert my .bat and .vbs to .py

2007-02-15 Thread Mark Bystry
Well, immediately I am having problems. Be patient with me.

This what I have...

copy_file.py


import os
import shutil as sh

sh.copy('C:\testing_it.txt', 'D:\')
raw_input("Done!")



...and it's not working. Obviously, I'm trying to copy a text file on C:\ to 
D:\.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Convert my .bat and .vbs to .py

2007-02-15 Thread Mark Bystry
You guys are great! I'm not sure how I found this mailing list but I'm glad 
that I subscribed.

Mark

Rob Andrews wrote the following on 2/15/2007 1:23 PM:
> We're good like that. heh
> 
> On 2/15/07, Mark Bystry <[EMAIL PROTECTED]> wrote:
>> Wow! Got a lot of responses to my post. Thanks everyone. After reading all 
>> of them, I may have to
>> learn by example after all.  I'm going to try Alan Gauld's tutorials first.
>>
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Convert my .bat and .vbs to .py

2007-02-15 Thread Mark Bystry
Wow! Got a lot of responses to my post. Thanks everyone. After reading all of 
them, I may have to 
learn by example after all.  I'm going to try Alan Gauld's tutorials first.

Mark

Alan Gauld wrote the following on 2/15/2007 12:39 PM:
> "Mark Bystry" <[EMAIL PROTECTED]> wrote
> 
>> My goal is to convert about a dozen or so DOS/Windows batch 
>> scripts(.bat) and vbscripts(.vbs) that I use on a day-to-day basis 
>> to python 
> 
> You might want to look at my Using the OS topic in my tutorial
> It covers basic file manipulation and starting external programs 
> etc.
> 
>> that they can be run on either my windows or linux workstations.
> 
> That might be harder than you think due to differences in paths etc.
> Also some external commands will be different, and ones with the 
> same name take different arguments/orders or args etc
> 
> If you write them in pure Python you should be OK but if you 
> just run sequences of OS commands (typical of batch files) 
> then you might run into problems.
> 
>> The first things that I want to learn is how to do basic stuff 
>> like copying, moving, and deleting files around from folder to 
>> folder. 
> 
> Try my tutor topic.
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Convert my .bat and .vbs to .py

2007-02-15 Thread Rob Andrews
I can confirm it works nicely in Windows. I have a script I use
several times daily to create working directories on a local
workstation by copying over arbitrarily deep directory trees into an
"original files" directory under a root directory named by job number.
The local workstation on which I have python running is a Windows
machine, and it's agnostic about the operating systems of the network
drives from which the data is gathered.

Quite handy and not even a very long program after the try/except
bulletproofing is added.

-Rob A.

On 2/15/07, Eric Walstad <[EMAIL PROTECTED]> wrote:
> Hey Mark, welcome aboard!
>
> There are a few different approaches you could take to convert your
> scripts.  If most of your scripts are related to copying/moving files,
> I'd have a look at Python's shutil module.  I think it'll work in both
> Windows and Linux but I don't have a Windows machine handy to test it.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Convert my .bat and .vbs to .py

2007-02-15 Thread Mike Hansen
 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Mark Bystry
> Sent: Thursday, February 15, 2007 10:04 AM
> To: tutor@python.org
> Subject: [Tutor] Convert my .bat and .vbs to .py
> 
> Hello,
> 
> This is my first post. I have just begun to code with python. 
> My goal is to convert about a dozen or 
> so DOS/Windows batch scripts(.bat) and vbscripts(.vbs) that I 
> use on a day-to-day basis to python so 
> that they can be run on either my windows or linux workstations.
> 
> The first things that I want to learn is how to do basic 
> stuff like copying, moving, and deleting 
> files around from folder to folder. I have been in the help 
> section of python (copyfile section) 
> but I do not understand what I am doing wrong.
> 
> 
> Here is my simple DOS batch file...
> 
> ##
> copy D:\PDF_Holding\*PRODUCTION.pdf Z:\
> copy D:\PDF_Holding\*ILLUSTRATION.pdf 
> H:\MARKETING\ILLUSTRATIONS\PRODUCT
> pause
> 
> del *.pdf
> ##
> 
> Basically, it copies the "production" and "illustration" pdf 
> files to certain folders than deletes 
> the left over pdf files when done. Yes, I could move them 
> instead of copying them.
> 
> I'm no asking for anyone to write this for me but if someone 
> could lead me i the right direction, I 
> would be grateful.
> 
> Thank-you.
> 
> Mark

Maybe there's a way to use shutil's copytree function with os module's
remove function or shutil's rmtree function? If not, use the glob module
to gather the file names and then walk the results of the glob with a
for loop and use shutil's copyfile function.

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


Re: [Tutor] Convert my .bat and .vbs to .py

2007-02-15 Thread Alan Gauld

"Mark Bystry" <[EMAIL PROTECTED]> wrote

> My goal is to convert about a dozen or so DOS/Windows batch 
> scripts(.bat) and vbscripts(.vbs) that I use on a day-to-day basis 
> to python 

You might want to look at my Using the OS topic in my tutorial
It covers basic file manipulation and starting external programs 
etc.

> that they can be run on either my windows or linux workstations.

That might be harder than you think due to differences in paths etc.
Also some external commands will be different, and ones with the 
same name take different arguments/orders or args etc

If you write them in pure Python you should be OK but if you 
just run sequences of OS commands (typical of batch files) 
then you might run into problems.

> The first things that I want to learn is how to do basic stuff 
> like copying, moving, and deleting files around from folder to 
> folder. 

Try my tutor topic.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

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


Re: [Tutor] Convert my .bat and .vbs to .py

2007-02-15 Thread Rob Andrews
If you import os, os.path, and shutil (only the ones you need,
although I wind up using all three for this sort of task), you can do
all that you have in mind and more.

os.path opens up some pretty painless methods for doing things like
testing to make sure the file exists in a given location before
copying it (or deleting it), which can save a good bit of heartache.

shutil can be used for not only copying files, but even copying entire
directory trees.

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


Re: [Tutor] Convert my .bat and .vbs to .py

2007-02-15 Thread Eric Walstad
Hey Mark, welcome aboard!

There are a few different approaches you could take to convert your
scripts.  If most of your scripts are related to copying/moving files,
I'd have a look at Python's shutil module.  I think it'll work in both
Windows and Linux but I don't have a Windows machine handy to test it.



Have fun,

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


Re: [Tutor] Convert my .bat and .vbs to .py

2007-02-15 Thread Tim Golden
Mark Bystry wrote:

> The first things that I want to learn is how to do basic stuff like copying, 
> moving, and deleting 
> files around from folder to folder. I have been in the help section of python 
> (copyfile section) 
> but I do not understand what I am doing wrong.

Welcome to Python. You probably want to look at the shutil
module, possibly combined with the os module:

http://docs.python.org/lib/module-shutil.html
http://docs.python.org/lib/os-file-dir.html

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