views:

486

answers:

4

Does anyone know of a way to make/read symbolic links across versions of win32 from Python? Ideally there should be a minimum amount of platform specific code, as I need my app to be cross platform.

+1  A: 
mobrule
There's only so much the os module can do: os.symlink() doesn't exist on Windows.
Ned Batchelder
I have it on Windows XP in v2.5.2. YMMV I guess.
mobrule
As the docs at http://docs.python.org/library/os.html?#os.symlink say about os.symlink: "Availability: Unix". SOME huge gaping holes of differences, such as this one, are just TOO huge to bridge easily.
Alex Martelli
This is going to change soon, take a look at http://bugs.python.org/issue1578269#
Sorin Sbarnea
A: 

Windows doesn't support symbolic links.

Ned Batchelder
It varies -- Vista does an ALMOST semi-quasi-usable job of them, 2k and xp have one very special case covered, and, there are shortcuts.
Alex Martelli
Vista, WS2003/2008, and Windows-7 NTFS support Symbolic links.
Cheeso
+4  A: 

Problem is, as explained e.g. here, that Windows' own support for the functionality of symbolic links varies across Windows releases, so that e.g. in Vista (with lots of work) you can get more functionality than in XP or 2000 (nothing AFAIK on other win32 versions). Or you could have shortcuts instead, which of course have their own set of limitations and aren't "really" equivalent to Unix symbolic links. So, you have to specify exactly what functionalities you require, how much of those you are willing to sacrifice on the altar of cross-win32 operation, etc -- THEN, we can work out how to implement the compromise you've chosen in terms of ctypes or win32all calls... that's the least of it, in a sense.

Alex Martelli
+2  A: 

NTFS file system has junction points, i think you may use them instead

copied from my answer to http://stackoverflow.com/questions/1143260/create-ntfs-junction-point-in-python

you can use python win32 API modules e.g.

import win32file

win32file.CreateSymbolicLink(fileSrc, fileTarget, 1)

see http://docs.activestate.com/activepython/2.5/pywin32/win32file%5F%5FCreateSymbolicLink%5Fmeth.html for more details

if you do not want to rely on that too, you can always use ctypes and directly call CreateSymbolicLinl win32 API, which is anyway a simple call

here is example call using ctypes

import ctypes

kdll = ctypes.windll.LoadLibrary("kernel32.dll")

kdll.CreateSymbolicLinkA("d:\test.txt", "d:\test_link.txt", 0)

MSDN (http://msdn.microsoft.com/en-us/library/aa363866%28VS.85%29.aspx) says Minimum supported client Windows Vista

Anurag Uniyal
You shouldn't use the `..A` Windows API, use `..W` (Unicode) instead - every time - even in examples such as this.
Sorin Sbarnea