使用Python从共享文件夹中递归提取所有文件

baubqpgj  于 2023-01-29  发布在  Python
关注(0)|答案(1)|浏览(161)

我已经使用Pywin32与Windows共享驱动器建立了连接

import win32net
data = {
    'remote': r"\\server\shared",
    'local': '',
    'username': 'username',
    'password': 'password'
}

win32net.NetUseAdd(None, 2, data)

我需要列出共享文件夹中存在的所有文件,类似于os.walk(path)
有哪些可能的方法可以做到这一点?

67up9zun

67up9zun1#

检查[ME.TimGolden]: Python for Win32 Extensions Help
((正式)由[GitHub]: mhammond/pywin32 - Python for Windows (pywin32) Extensions引用,[GitHub]: mhammond/pywin32 - Python for Windows (pywin32) Extensions是 * WinAPI * 的 * Python * Package 器)。

[MS.Learn]: USE_INFO_2 structure (lmuse.h)(由您使用的[ME.TimGolden]: PyUSE_INFO_2 Object Package )声明:
ui2_local
类型:LMSTR
指向字符串的指针,该字符串包含要重定向到共享资源的本地设备名称(例如,驱动器E或LPT1)。常量DEVLEN指定字符串中的最大字符数。此成员可以是NULL。有关详细信息,请参阅下面的"备注"部分。
你要做的就是:
1.* * 提供本地名称**(未使用)
1.一旦连接成功,使用它(就像任何"常规"本地驱动器一样)

  • 代码00.py *:
#!/usr/bin/env python

import os
import sys

import win32net as wnet

def main(*argv):
    local_drive = "y:"
    data = {
        "remote": r"\\localhost\share_cfati",
        "local": local_drive,
        "username": "user",  # @TODO - cfati: Modified to hide sensitive data
        "password": "***",  # @TODO - cfati: Modified to hide sensitive data
    }

    try:
        res = wnet.NetUseAdd(None, 2, data)
        #print(res)
    except:
        print("Error adding connection:", sys.exc_info())
        return -1

    print("Items in the shared folder:\n{:}".format(os.listdir(local_drive)))

if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.\n")
    sys.exit(rc)
    • 输出**:
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075267137]> sopr.bat
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[prompt]>
[prompt]> net share

Share name   Resource                        Remark

-------------------------------------------------------------------------------
ADMIN$       C:\WINDOWS                      Remote Admin
C$           C:\                             Default share
E$           E:\                             Default share
F$           F:\                             Default share
G$           G:\                             Default share
H$           H:\                             Default share
IPC$                                         Remote IPC
L$           L:\                             Default share
M$           M:\                             Default share
N$           N:\                             Default share
share_cfati  L:\Share\cfati
share_pub_ro L:\Share\pub_ro
share_pub_rw L:\Share\pub_rw
The command completed successfully.

[prompt]>
[prompt]> net use
New connections will not be remembered.

There are no entries in the list.

[prompt]>
[prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\python.exe" ./code00.py
Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec  6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] 064bit on win32

Items in the shared folder:
['.DS_Store', 'a.py', 'b', 'c', 'code00.py', 'commander.dmg', 'copy_sdk-aars.sh', 'cstrike1.6.zip', 'dedus', 'doublecmd.xml', 'ifm', 'pula', 'py2713.tgz']

Done.

[prompt]>
[prompt]> net use
New connections will not be remembered.

Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK           Y:        \\localhost\share_cfati   Microsoft Windows Network
The command completed successfully.

[prompt]>
[prompt]> net use /delete y:
y: was deleted successfully.

[prompt]>
[prompt]> net use
New connections will not be remembered.

There are no entries in the list.
    • 备注**:
  • 这相当于 * WinUI * 中的*Map网络驱动器...***功能
  • 一般来说,当完成一个资源时,最好释放它。我删除了连接(net use /delete y:),但这也可以通过win32net.NetUseDel * 从代码中完成
  • 文件(文件夹)浏览本身不是这个问题的一部分(至少我不认为它是),因为它是一个不同的(独立的)主题。

相关问题