在WindowswithPython2.7下,是否有方法检查某个文件夹是否是任何连接点的目标?如果是的话,找出哪一个符号链接通向它?
例如,在cmd中,shell使用mklink创建一个连接点。
C:\>mklink /J C:\junction C:\Users
Junction created for C:\junction <<===>> C:\Users在python中(假设不知道存在此连接)测试"C:\Users"是否是任何连接点的目标,如果True返回符号链接列表,在本例中是:['C:\junction']。
发布于 2014-02-04 19:48:49
这看起来很难有效地完成,但是您可以扫描一个卷上所有现有的连接点(参见point“获取接入点列表”)。然后,你必须把它们和它们所指的东西联系起来。目录本身并不指向现有的连接点,因此您在如何找到这些连接方面非常有限。
发布于 2014-02-05 00:49:20
下面是我使用ActiveState菜谱中名为http://code.activestate.com/recipes/578629-windows-directory-walk-using-ctypes/的一些代码组合起来的东西。可能有一种比使用win32 FindFirstFile和FindNextFile函数更直接的方法,但这似乎适用于我有限的测试。
import os
import sys
import ctypes
from ctypes import Structure
from ctypes import byref
import ctypes.wintypes as wintypes
from ctypes import addressof
FILE_ATTRIBUTE_DIRECTORY = 16 # (0x10)
FILE_ATTRIBUTE_REPARSE_POINT = 1024 # (0x400)
MAX_PATH = 260
GetLastError = ctypes.windll.kernel32.GetLastError
class FILETIME(Structure):
_fields_ = [("dwLowDateTime", wintypes.DWORD),
("dwHighDateTime", wintypes.DWORD)]
class WIN32_FIND_DATAW(Structure):
_fields_ = [("dwFileAttributes", wintypes.DWORD),
("ftCreationTime", FILETIME),
("ftLastAccessTime", FILETIME),
("ftLastWriteTime", FILETIME),
("nFileSizeHigh", wintypes.DWORD),
("nFileSizeLow", wintypes.DWORD),
("dwReserved0", wintypes.DWORD),
("dwReserved1", wintypes.DWORD),
("cFileName", wintypes.WCHAR * MAX_PATH),
("cAlternateFileName", wintypes.WCHAR * 20)]
def find_junctions(folder):
""" Return a list of subdirectories in folder which are junction points """
if not os.path.isdir(folder):
return False
folder = unicode(folder)
if not folder.startswith(u'\\\\?\\'):
if folder.startswith(u'\\\\'):
# network drive
folder = u'\\\\?\\UNC' + folder[1:]
else:
# local drive
folder = u'\\\\?\\' + folder
junction_points = []
data = WIN32_FIND_DATAW()
h = ctypes.windll.kernel32.FindFirstFileW(os.path.join(folder, u'*'),
byref(data))
last_error = ctypes.windll.kernel32.GetLastError()
if h < 0:
ctypes.windll.kernel32.FindClose(h)
if not sys.stderr.isatty():
print >> sys.stderr, ('Failed to find first file %s' %
os.path.join(folder, u'*'))
if last_error != 5: # access denied.
raise WindowsError('FindFirstFileW %s, Error: %d' %
(folder, ctypes.windll.kernel32.GetLastError()))
return []
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY and
data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT):
if data.cFileName not in (u'.', u'..'):
junction_points.append(data.cFileName[:])
try:
while ctypes.windll.kernel32.FindNextFileW(h, byref(data)):
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY and
data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT):
if data.cFileName not in (u'.', u'..'):
junction_points.append(data.cFileName[:])
except WindowsError as e:
if not sys.stderr.isatty():
print >> sys.stderr, (
'Failed to find next file %s, handle %d, buff addr: 0x%x' %
(os.path.join(folder, u'*'), h, addressof(data)))
ctypes.windll.kernel32.FindClose(h)
return junction_points
def is_junction_point(folder):
dirpath, folder = os.path.split(os.path.abspath(folder))
return unicode(folder) in find_junctions(dirpath)https://stackoverflow.com/questions/21561850
复制相似问题