porting python script from linux to windows

H

hokieghal99

What are the specific steps that one should take to make a python script
that works on a Linux x86 machine also work on a Windows x86 machine?

I am using os, re and string in the script. How do paths differ between
the two OSes? I am use to a unix-like path /blah/blah/blah. Would I need
to change this to x:\blah\blah\blah where x = drive letter? What about
creating fs objects? file works the same on both? os.walk works the same?

Thanks for the advice!!!
 
R

Rene Pijlman

hokieghal99:
What are the specific steps that one should take to make a python script
that works on a Linux x86 machine also work on a Windows x86 machine?

Check for platform dependencies in the documentation of all modules that
you use.
I am using os, re and string in the script. How do paths differ between
the two OSes? I am use to a unix-like path /blah/blah/blah. Would I need
to change this to x:\blah\blah\blah where x = drive letter?

I'm currently developing an app on Windows and testing it for production
on Linux. I use os.path.join() to construct pathnames and have encountered
no portability problems.
What about creating fs objects? file works the same on both?

Yes. But note the advice about binary files in the documentation of the
open()/file() builtin function.
os.walk works the same?

It works :)
 
B

BW Glitch

hokieghal99 said:
What are the specific steps that one should take to make a python script
that works on a Linux x86 machine also work on a Windows x86 machine?

I am using os, re and string in the script. How do paths differ between
the two OSes? I am use to a unix-like path /blah/blah/blah. Would I need
to change this to x:\blah\blah\blah where x = drive letter? What about
creating fs objects? file works the same on both? os.walk works the same?

Instead of using full paths, try using the os.path.join method. It makes
it easier to port from/to any OS.

--
Glitch

-----BEGIN TF FAN CODE BLOCK-----
G+++ G1 G2+ BW++++ MW++ BM+ Rid+ Arm-- FR+ FW-
#3 D+ ADA N++ W OQP MUSH- BC- CN++ OM P75
-----END TF FAN CODE BLOCK-----

"So you failed."
"Absolutely!"
"Completely."
-- Optimus Primal, Rattrap, and Airazor, "Before the Storm"
 
D

Dan Bishop

hokieghal99 said:
What are the specific steps that one should take to make a python script
that works on a Linux x86 machine also work on a Windows x86 machine?

I am using os, re and string in the script. How do paths differ between
the two OSes? I am use to a unix-like path /blah/blah/blah. Would I need
to change this to x:\blah\blah\blah where x = drive letter?

You woudn't *need* to; the drive letter can be omitted when the file
is on the same drive. Also, Windows doesn't care what kind of slashes
you use (except for cmd.exe).

But you will have to, for example, change "/home/dan" to
"c:\\winnt\\Documents and Settings\\dan".
What about
creating fs objects? file works the same on both?

For the most part, yes, but in mind that, on Windows, it matters
whether you open a file in text or binary mode.
os.walk works the same?

Yes.
 
H

Hans Nowak

hokieghal99 said:
What are the specific steps that one should take to make a python script
that works on a Linux x86 machine also work on a Windows x86 machine?

Run it, and see what happens. ;-) Seriously, in theory there is no reason why
it shouldn't work right out of the box, unless you have platform-specific code.
So I'd say, just run it and see if you encounter any problems.
I am using os, re and string in the script. How do paths differ between
the two OSes? I am use to a unix-like path /blah/blah/blah. Would I need
to change this to x:\blah\blah\blah where x = drive letter?

Maybe. Paths with slashes can be used on Windows as well.
What about
creating fs objects? file works the same on both? os.walk works the same?

Files should work the same, except that on Windows opening files in 'binary
mode' is different from 'text mode'. os.walk should work just fine.

HTH,
 

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
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top