Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using os.path for POSIX Path Operations on Windows

I'm using Paramiko on Windows to access a remote SFTP server. I need to do some work with remote paths like os.path.join, os.path.commonprefix, etc. Since my host platform is Windows, all paths operations come with the \ separator, but I need POSIX-styled paths.

Is there any way to use Python built-in POSIX path operations on Windows?

like image 596
Vasilly.Prokopyev Avatar asked Apr 13 '16 08:04

Vasilly.Prokopyev


1 Answers

As pointed out in the documentation (2nd note)

... you can also import and use the individual modules if you want to manipulate a path that is always in one of the different formats. They all have the same interface:

  • posixpath for UNIX-style paths
  • ntpath for Windows paths

So you could import posixpath and use it as os.path

>>> import posixpath
>>> posixpath.join
<function join at 0x025C4170>
>>> posixpath.join('a','b')
'a/b'
>>> posixpath.commonprefix
<function commonprefix at 0x00578030>
like image 122
Francesco Avatar answered Nov 10 '22 21:11

Francesco