1

Problem

Imagine you are writing a Python script which runs on cygwin and calls an external C# executable which requires as input a path. Assume you cannot change the C# executable in any way. As you send the path you want to the executable, it rejects all cygwin paths.

So if you pass the path /cygdrive/c/location/of/file.html as a POSIX path, it will fail as the executable requires a Windows path like C:\location\of\file.html

Example:

Message location = os.path.dirname(os.path.realpath(__file__)) os.system('./cSharpScript.exe ' + message_location)

Will result in:

File for the content (/cygdrive/c/location/of/file.html) not found.

Things I've tried so far:

PATH = /cygdrive/c/location/of/file.html

1) path = PATH.replace('/','\\')

Result: File for the content (cygdriveclocationoffile.html) not found.

2) path = os.path.abspath(PATH)

Result: File for the content (/cygdrive/c/location/of/file.html) not found.

  • os.path.realpath has the same results

I'm probably going in a completely wrong direction with my solutions so far... How would you handle it?

Mike Meyers
  • 115
  • 5
  • The Windows path is `r'C:\location\of\file.html'`, where `"/cygdrive/c"` -> `"C:"` and slashes translate to backslashes. – Eryk Sun Jan 17 '19 at 14:39
  • 1
    Yeah, but I'd hate to do it manually... Does python have some way of dealing with it? – Mike Meyers Jan 17 '19 at 15:00
  • Not in the standard library; maybe there's a package for it on PyPI. Or maybe Cygwin has a system function or command-line utility to convert paths. – Eryk Sun Jan 17 '19 at 15:06
  • Fun fact: Windows itself has to translate "C:" to a name in the caller's local device directory. This is referenced as the magic prefix "\??\", such as "\??\C:\location\of\file.html". It resolves to the caller's logon-session mountpoint directory, "\Sessions\0\DosDevices\", which shadows (in a union) the global mount directory, "\Global??". These contain symbolic links (AKA junctions) with well-known names that target devices that are usually in "\Device". The real NT path is thus something like "\Device\HarddiskVolume2\location\of\file.html". But maybe that's a lie, too... – Eryk Sun Jan 17 '19 at 15:08
  • 1
    Perhaps this article may be helpful. [Convert POSIX->WIN path, in Cygwin Python, w/o calling cygpath](https://stackoverflow.com/q/10884268/9014308) – kunif Jan 17 '19 at 15:38

1 Answers1

3

According to [Cygwin]: cygpath:

cygpath - Convert Unix and Windows format paths, or output system path information
...

-w, --windows         print Windows form of NAMEs (C:\WINNT)

Example:

[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q054237800]> cygpath.exe -w /cygdrive/c/location/of/file.html
C:\location\of\file.html

Translated into Python (this is a rough version, only for demonstrating purposes):

>>> import subprocess
>>>
>>>
>>> def get_win_path(cyg_path):
...     return subprocess.check_output(["cygpath", "-w", cyg_path]).strip(b"\n").decode()
...
>>>
>>> print(get_win_path("/cygdrive/c/location/of/file.html"))
C:\location\of\file.html
CristiFati
  • 38,250
  • 9
  • 50
  • 87