1

I want my Project, built in PYTHON to support Windows Linux MAC in any terminal(MAC, CYGWIN, Windows etc). I can able to achieve but I am facing some issue When I run the following

import os
mypath = os.path.join('users','scripts', 'pythonsample')
print mypath

In windows command prompt output is

users\scripts\pythonsample

In MAC terminal output is

users/scripts/pythonsample

Also, when I run the following code

import glob
glob.glob(os.path.join('users','scripts', 'pythonsample','*.*'))

In windows command prompt output is

[users/scripts\\pythonsample\\a1.py,
users/scripts\\pythonsample\\a2.py,
users/scripts\\pythonsample\\a3.py
users/scripts\\pythonsample\\a4.py]

In MAC terminal output is

[users/scripts/pythonsample/a1.py,
users/scripts/pythonsample/a2.py,
users/scripts/pythonsample/a3.py
users/scripts/pythonsample/a4.py]

So to parse and get get the name of the file without whole path becomes difficult in multiple platforms.

I can write a if else block to decide whether the script is running in Windows or MAC or CGYWIN.

import sys
#Output of below command is Win32, linux2, darwin, cgywin 
print(sys.platform)

but is there a easy way to accomplish this with out if else block?

3
  • What output do you actually want? Do you want POSIX-style paths even on Windows? Do you want to get Windows-style paths always instead of just in most cases on Windows? Or something different? And, most importantly, please explain why you want that. What's wrong with what you're currently getting? Commented Aug 9, 2013 at 0:59
  • If glob.glob(somepath) output is same across the OS, then I can split the path using string.split('\\'). I will have to change this to string.split('\\') in windows string.split('/') in MAC. Commented Aug 9, 2013 at 1:05
  • 1
    Why are you using string.split? You're already using os.path; use its methods to split things too! Commented Aug 9, 2013 at 1:13

1 Answer 1

5

This is exactly what you should expect. On Windows, os.path gives you Windows-style paths; on Mac OS X, it gives you POSIX-style paths.

If you're looking to guarantee POSIX paths everything, don't use os.path at all, use posixpath instead.

On the other hand, if you've got paths that may be in POSIX format even on Windows (since most parts of Windows handle POSIX-style paths, and many tools generate POSIX-style paths) and want to guarantee that you've got a native path, call os.path.normpath.

Sign up to request clarification or add additional context in comments.

4 Comments

If glob.glob(somepath) output is same across the OS, then I can split the path using string.split('\\'). I will have to change this to string.split('\\') in windows string.split('/') in MAC.
You can always s.split(os.pathsep) if you want, which will split on backslashes on Windows and slashes everywhere else. But the whole point of os.path is that you don't have to deal with this nonsense. Just use the methods in os.path to deal with paths.
os.pathsep is not working in Windows. >>> c = os.path.abspath('..') >>> c.split(os.pathsep) ['C:\\Users\\Admin\\Desktop']
@Venkatesh: Sorry, yeah, I meant os.sep. Thanks for the correction; that could easily confuse future readers otherwise!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.