windows / unix path

M

Marcin201

Is there an built-in functionality in python to convert Windows paths
to Unix paths? I am running into problems when creating data files on
Windows and the running them on a Unix platform. I create paths using
os.path.join.

os.path.join('Pictures', '01.jpg') returns 'Pictures\\01..jpg' on
Win. When I read files created on Win under Unix this is a problem,
python cannot open 'Pictures\\01.jpg'

Thanks,

Marcin
 
J

John Machin

Is there an built-in functionality in python to convert Windows paths
to Unix paths?  I am running into problems when creating data files on
Windows and the running them on a Unix platform.  I create paths using
os.path.join.

os.path.join('Pictures', '01.jpg') returns 'Pictures\\01..jpg' on
Win.  When I read files created on Win under Unix this is a problem,
python cannot open 'Pictures\\01.jpg'

Note that os.path.join('Pictures', '01.jpg') returns 'Pictures/01.jpg'
on Unix.
Note that 'Pictures\\01.jpg' == r'Pictures\01.jpg' i.e. there is only
one backslash.
Have you considered unix_path = windows_path.replace('\\', '/')?
 
L

Lawrence D'Oliveiro

In message
os.path.join('Pictures', '01.jpg') returns 'Pictures\\01..jpg' on
Win. When I read files created on Win under Unix this is a problem,
python cannot open 'Pictures\\01.jpg'

But it can on Windows, right?

os.path contains functions specific to the _current_ platform (the one your
script is running on). If you're trying to perform pathname manipulations
on behalf of another platform, you shouldn't be using os.path.
 
L

Larry Bates

Marcin201 said:
Is there an built-in functionality in python to convert Windows paths
to Unix paths? I am running into problems when creating data files on
Windows and the running them on a Unix platform. I create paths using
os.path.join.

os.path.join('Pictures', '01.jpg') returns 'Pictures\\01..jpg' on
Win. When I read files created on Win under Unix this is a problem,
python cannot open 'Pictures\\01.jpg'

Thanks,

Marcin

I use posixpath when I want to "force" forward slashes that I know will work on
Linux. Actually the forward slashes work fine on Windows also (undocumented
feature of Windows).

-Larry
 
M

MRAB

I use posixpath when I want to "force" forward slashes that I know will work on
Linux.  Actually the forward slashes work fine on Windows also (undocumented
feature of Windows).
FYI, in Windows the standard is for commandline options to begin with
a slash, eg dir /b, but as long as the path doesn't begin with one
you'll be OK. :)
 
G

greg

Dennis said:
The command line is the only place the slash direction has any
effect any way... Avoid os.system(), subprocess with shell = True, and
forward is safe in any position.

I'm not sure that's quite true. On Windows, it's not the
shell that splits up the command line into arguments, it's
the program being run. So you need to avoid any kind of
exec or spawn operation, whether it goes through the shell
or not, unless you know the program being run isn't going
to interpret '/' as an option.
 
T

Tim Roberts

greg said:
I'm not sure that's quite true. On Windows, it's not the
shell that splits up the command line into arguments, it's
the program being run. So you need to avoid any kind of
exec or spawn operation, whether it goes through the shell
or not, unless you know the program being run isn't going
to interpret '/' as an option.

Well, you actually said the same thing as Dennis here, in a slightly
different way.

The executive summary here is that the Windows APIs all accept either
forward or backward slashes just fine. The trouble happens when you start
using command lines, just as Dennis said.
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top