Nếu tôi sử dụng các mục sau để lấy danh sách tất cả các ổ đĩa được kết nối:
available_drives = ['%s:' % d for d in string.ascii_uppercase if os.path.exists['%s:' % d]]
Làm thế nào để tôi có được đường dẫn UNC của các ổ đĩa được kết nối?
import win32wnet
import sys
print[win32wnet.WNetGetUniversalName[sys.argv[1], 1]]
2 Chỉ trả về import win32wnet
import sys
print[win32wnet.WNetGetUniversalName[sys.argv[1], 1]]
3 thay vì import win32wnet
import sys
print[win32wnet.WNetGetUniversalName[sys.argv[1], 1]]
4Đã hỏi ngày 7 tháng 7 năm 2017 lúc 12:24Jul 7, 2017 at 12:24
3
Sử dụng Win32WNet từ PyWin32 để chuyển đổi các chữ cái ổ đĩa của bạn. Ví dụ:
import win32wnet
import sys
print[win32wnet.WNetGetUniversalName[sys.argv[1], 1]]
Điều này mang lại cho tôi một cái gì đó như thế này khi tôi chạy nó:
C:\test>python get_unc.py i:\some\path
\\machine\test_share\some\path
Đã trả lời ngày 14 tháng 7 năm 2017 lúc 12:10Jul 14, 2017 at 12:10
Peter Brittainpeter BrittainPeter Brittain
13.3k3 Huy hiệu vàng40 Huy hiệu bạc54 Huy hiệu đồng3 gold badges40 silver badges54 bronze badges
3
Đây là cách làm điều đó trong Python ≥ 3,4, không có sự phụ thuộc!*
from pathlib import Path
def unc_drive[file_path]:
return str[Path[file_path].resolve[]]
*Lưu ý: Tôi vừa tìm thấy một tình huống trong đó phương pháp này thất bại. Một trong những cổ phiếu mạng của công ty tôi có thiết lập quyền sao cho phương pháp này tăng
import win32wnet
import sys
print[win32wnet.WNetGetUniversalName[sys.argv[1], 1]]
5. Trong trường hợp này, import win32wnet
import sys
print[win32wnet.WNetGetUniversalName[sys.argv[1], 1]]
6 là một dự phòng phù hợp.Đã trả lời ngày 1 tháng 8 năm 2019 lúc 0:00Aug 1, 2019 at 0:00
Sử dụng
import win32wnet
import sys
print[win32wnet.WNetGetUniversalName[sys.argv[1], 1]]
7 và mã được hiển thị trong câu trả lời đầu tiên trong bài đăng này: Nhận tên máy tính đầy đủ từ ký tự ổ đĩa mạng trong Python, có thể nhận đường dẫn ổ đĩa cho mỗi ổ đĩa mạng hoặc một số ít được chọn.Hàm
import win32wnet
import sys
print[win32wnet.WNetGetUniversalName[sys.argv[1], 1]]
8 được đưa ra sẽ gây ra lỗi nếu ổ đĩa không phải là ổ đĩa mạng, các ổ đĩa cục bộ hoặc có thể tháo rời, điều này có thể được tính# your drive list
available_drives = ['%s:' % d for d in string.ascii_uppercase if os.path.exists['%s:' % d]]
for drive in available_drives:
try:
# function from linked post
print[get_connection[drive]]
except WindowsError: # thrown from local drives
print['{} is a local drive'.format[drive]]
Đã trả lời ngày 10 tháng 7 năm 2017 lúc 15:31Jul 10, 2017 at 15:31
NJOOSSENJOOSSEnjoosse
5493 Huy hiệu bạc8 Huy hiệu Đồng3 silver badges8 bronze badges
Vấn đề37993
Được tạo vào năm 2019-08-31 08:04 bởi CgoHlke, lần cuối đã thay đổi 2022-04-11 14:59 bởi Quản trị viên. Vấn đề giờ đã kết thúc.2019-08-31 08:04 by cgohlke, last changed 2022-04-11 14:59 by admin. This issue is now closed.
Re //bugs.python.org/issue9949: Is it intended that Python-3.8.0b4 now also resolves mapped network drives and drives created with `subst`? I would not expect this from the documentation at //docs.python.org/3.8/library/os.path.html#os.path.realpath. The documentation refers to symbolic links and junctions, which are different from mapped network and subst drives [AFAIU]. For example, mapping `\\SERVER\Programs` as `X:` drive: ``` Python 3.8.0b4 [tags/v3.8.0b4:d93605d, Aug 29 2019, 23:21:28] [MSC v.1916 64 bit [AMD64]] on win32 >>> import sys, os >>> sys.executable 'X:\\Python38\\python.exe' >>> os.path.realpath[sys.executable] '\\\\SERVER\\Programs\\Python38\\python.exe' ``` ``` Python 3.7.4 [tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20] [MSC v.1916 64 bit [AMD64]] on win32 >>> import sys, os >>> sys.executable 'X:\\Python37\\python.exe' >>> os.path.realpath[sys.executable] 'X:\\Python37\\python.exe' ``` It seems this change causes an error in pytest-5.1.2 during numpy-1.17.1 tests: ``` X:\Python38>python.exe -c"import numpy;numpy.test[]" NumPy version 1.17.1 NumPy relaxed strides checking option: True ============================================= ERRORS ============================================== __________________________________ ERROR collecting test session __________________________________ lib\site-packages\_pytest\config\__init__.py:440: in _importconftest return self._conftestpath2mod[conftestpath] E KeyError: local['\\\\SERVER\\programs\\python38\\lib\\site-packages\\numpy\\conftest.py'] During handling of the above exception, another exception occurred: lib\site-packages\_pytest\config\__init__.py:446: in _importconftest mod = conftestpath.pyimport[] lib\site-packages\py\_path\local.py:721: in pyimport raise self.ImportMismatchError[modname, modfile, self] E py._path.local.LocalPath.ImportMismatchError: ['numpy.conftest', 'X:\\Python38\\lib\\site-packages\\numpy\\conftest.py', local['\\\\SERVER\\programs\\python38\\lib\\site-packages\\numpy\\conftest.py']] During handling of the above exception, another exception occurred: lib\site-packages\_pytest\runner.py:220: in from_call result = func[] lib\site-packages\_pytest\runner.py:247: in call = CallInfo.from_call[lambda: list[collector.collect[]], "collect"] lib\site-packages\_pytest\main.py:485: in collect yield from self._collect[arg] lib\site-packages\_pytest\main.py:512: in _collect col = self._collectfile[pkginit, handle_dupes=False] lib\site-packages\_pytest\main.py:581: in _collectfile ihook = self.gethookproxy[path] lib\site-packages\_pytest\main.py:424: in gethookproxy my_conftestmodules = pm._getconftestmodules[fspath] lib\site-packages\_pytest\config\__init__.py:420: in _getconftestmodules mod = self._importconftest[conftestpath] lib\site-packages\_pytest\config\__init__.py:454: in _importconftest raise ConftestImportFailure[conftestpath, sys.exc_info[]] E _pytest.config.ConftestImportFailure: [local['\\\\SERVER\\programs\\python38\\lib\\site-packages\\numpy\\conftest.py'], [, ImportMismatchError['numpy.conftest', 'X:\\Python38\\lib\\site-packages\\numpy\\conftest.py', local['\\\\SERVER\\programs\\python38\\lib\\site-packages\\numpy\\conftest.py']], ]] !!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!! 1 error in 16.39s ``` |
> >>> sys.executable > 'X:\\Python38\\python.exe' > >>> os.path.realpath[sys.executable] > '\\\\SERVER\\Programs\\Python38\\python.exe' Unix Python resolves the executable path with repeated _Py_wreadlink calls. Windows Python should do something similar to ensure the consistency of sys.executable with realpath[sys.executable]. > different from mapped network and subst drives [AFAIU]. Mapped and subst drives are implemented as object-manager symlinks to file-system directories. For example, a subst drive "S:" might target a local directory such as r"\??\C:\Temp\Subst", and a mapped drive "M:" for an SMB share might target a path such as r"\Device\LanmanRedirector\;M:\Server\Share\Temp\Mapped". The root directory of these drives does not behave like a real root directory unless the drive targets the root of a volume or UNC share, such as "\\??\\C:\\" or r"\Device\LanmanRedirector;M:\Server\Share". This means that in many cases it's possible to evaluate a relative symlink that traverses above the drive root via ".." components. Say we have a directory r"C:\Temp\Subst" that contains a relative symlink "foo_link" that targets r"..\foo". If we map "S:" to r"C:\Temp\Subst", then r"S:\foo_link" opens r"C:\Temp\foo". Similarly if we map r"\\localhost\C$\Temp\Subst" to "M:", then r"M:\foo_link" opens r"C:\Temp\foo". In the above case, if we're using relpath[] to compute the relative path to the "foo" target, I think we want relpath[realpath['C:/Temp/foo'], realpath['S:/']] to succeed as r"..\foo". I don't think we want it to fail as a cross-drive relative path. |
Is this an issue or a mismatched expectation? Tests that assume realpath[] on Windows is the equivalent of abspath[] are of course going to fail when we fix realpath[], and that's kind of what this one looks like. Just because it doesn't have a direct Unix equivalent doesn't mean that any particular behavior is any better. > Unix Python resolves the executable path with repeated _Py_wreadlink calls. Windows Python should do something similar to ensure the consistency of sys.executable with realpath[sys.executable]. I don't think this necessarily follows. There's nowhere in the documentation that says that sys.executable is even a valid path, let alone the final path. > I think we want relpath[realpath['C:/Temp/foo'], realpath['S:/']] to succeed as r"..\foo". I don't think we want it to fail as a cross-drive relative path. Cross-drive relative paths are fine though - they are just absolute paths :] > The documentation refers to symbolic links and junctions, which are different from mapped network and subst drives [AFAIU]. If we can easily tell the difference between directory junctions and mapped drives, given that they are both identical types of reparse points, then we can make readlink[] only read directory junctions. I need a specific algorithm for telling the difference though, not just lists of examples of things that "should" work [without any rationale for why they ought to work]. |
Is this an issue or a mismatched expectation? Tests that assume realpath[] on Windows is the equivalent of abspath[] are of course going to fail when we fix realpath[], and that's kind of what this one looks like. Just because it doesn't have a direct Unix equivalent doesn't mean that any particular behavior is any better. > Unix Python resolves the executable path with repeated _Py_wreadlink calls. Windows Python should do something similar to ensure the consistency of sys.executable with realpath[sys.executable]. I don't think this necessarily follows. There's nowhere in the documentation that says that sys.executable is even a valid path, let alone the final path. > I think we want relpath[realpath['C:/Temp/foo'], realpath['S:/']] to succeed as r"..\foo". I don't think we want it to fail as a cross-drive relative path. Cross-drive relative paths are fine though - they are just absolute paths :] > The documentation refers to symbolic links and junctions, which are different from mapped network and subst drives [AFAIU]. If we can easily tell the difference between directory junctions and mapped drives, given that they are both identical types of reparse points, then we can make readlink[] only read directory junctions. I need a specific algorithm for telling the difference though, not just lists of examples of things that "should" work [without any rationale for why they ought to work]. |
> >>> sys.executable > 'X:\\Python38\\python.exe' > >>> os.path.realpath[sys.executable] > '\\\\SERVER\\Programs\\Python38\\python.exe' Unix Python resolves the executable path with repeated _Py_wreadlink calls. Windows Python should do something similar to ensure the consistency of sys.executable with realpath[sys.executable]. > different from mapped network and subst drives [AFAIU]. Mapped and subst drives are implemented as object-manager symlinks to file-system directories. For example, a subst drive "S:" might target a local directory such as r"\??\C:\Temp\Subst", and a mapped drive "M:" for an SMB share might target a path such as r"\Device\LanmanRedirector\;M:\Server\Share\Temp\Mapped". The root directory of these drives does not behave like a real root directory unless the drive targets the root of a volume or UNC share, such as "\\??\\C:\\" or r"\Device\LanmanRedirector;M:\Server\Share". This means that in many cases it's possible to evaluate a relative symlink that traverses above the drive root via ".." components. Say we have a directory r"C:\Temp\Subst" that contains a relative symlink "foo_link" that targets r"..\foo". If we map "S:" to r"C:\Temp\Subst", then r"S:\foo_link" opens r"C:\Temp\foo". Similarly if we map r"\\localhost\C$\Temp\Subst" to "M:", then r"M:\foo_link" opens r"C:\Temp\foo". In the above case, if we're using relpath[] to compute the relative path to the "foo" target, I think we want relpath[realpath['C:/Temp/foo'], realpath['S:/']] to succeed as r"..\foo". I don't think we want it to fail as a cross-drive relative path. |
Is this an issue or a mismatched expectation? Tests that assume realpath[] on Windows is the equivalent of abspath[] are of course going to fail when we fix realpath[], and that's kind of what this one looks like. Just because it doesn't have a direct Unix equivalent doesn't mean that any particular behavior is any better. > Unix Python resolves the executable path with repeated _Py_wreadlink calls. Windows Python should do something similar to ensure the consistency of sys.executable with realpath[sys.executable]. I don't think this necessarily follows. There's nowhere in the documentation that says that sys.executable is even a valid path, let alone the final path. > I think we want relpath[realpath['C:/Temp/foo'], realpath['S:/']] to succeed as r"..\foo". I don't think we want it to fail as a cross-drive relative path. Cross-drive relative paths are fine though - they are just absolute paths :] > The documentation refers to symbolic links and junctions, which are different from mapped network and subst drives [AFAIU]. If we can easily tell the difference between directory junctions and mapped drives, given that they are both identical types of reparse points, then we can make readlink[] only read directory junctions. I need a specific algorithm for telling the difference though, not just lists of examples of things that "should" work [without any rationale for why they ought to work]. |
> >>> sys.executable > 'X:\\Python38\\python.exe' > >>> os.path.realpath[sys.executable] > '\\\\SERVER\\Programs\\Python38\\python.exe' Unix Python resolves the executable path with repeated _Py_wreadlink calls. Windows Python should do something similar to ensure the consistency of sys.executable with realpath[sys.executable]. > different from mapped network and subst drives [AFAIU]. Mapped and subst drives are implemented as object-manager symlinks to file-system directories. For example, a subst drive "S:" might target a local directory such as r"\??\C:\Temp\Subst", and a mapped drive "M:" for an SMB share might target a path such as r"\Device\LanmanRedirector\;M:\Server\Share\Temp\Mapped". The root directory of these drives does not behave like a real root directory unless the drive targets the root of a volume or UNC share, such as "\\??\\C:\\" or r"\Device\LanmanRedirector;M:\Server\Share". This means that in many cases it's possible to evaluate a relative symlink that traverses above the drive root via ".." components. Say we have a directory r"C:\Temp\Subst" that contains a relative symlink "foo_link" that targets r"..\foo". If we map "S:" to r"C:\Temp\Subst", then r"S:\foo_link" opens r"C:\Temp\foo". Similarly if we map r"\\localhost\C$\Temp\Subst" to "M:", then r"M:\foo_link" opens r"C:\Temp\foo". In the above case, if we're using relpath[] to compute the relative path to the "foo" target, I think we want relpath[realpath['C:/Temp/foo'], realpath['S:/']] to succeed as r"..\foo". I don't think we want it to fail as a cross-drive relative path. |
Ngày: 2019-09-01 02:42 | ________số 8 | bộ | MSG350946 - [Xem] |
Ngày: 2019-09-01 11:16 | Eryksun | bộ | Nosy: + eryksunmessages: + msg350916 |
2019-08-31 & NBSP; 08: 04: 35 | Steve.Dower | bộ | Tin nhắn: + MSG350936 resolution: not a bug messages: + msg351780 2019-08-31 & NBSP; 12: 54: 25 |
Eryksun | Eryksun | bộ | Nosy: + eryksunmessages: + msg350916 |
2019-08-31 & NBSP; 08: 04: 35 | Steve.Dower | bộ | Tin nhắn: + MSG350937 |
2019-09-01 & NBSP; 02: 41: 45 | Steve.Dower | bộ | Tin nhắn: + MSG350936 |
2019-08-31 & NBSP; 12: 54: 25 | Eryksun | bộ | Tin nhắn: + MSG350936 messages: + msg350916 |
2019-08-31 & NBSP; 12: 54: 25 | Eryksun | Nosy: + eryksunmessages: + msg350916 |