簡體   English   中英

pysmb遞歸刪除文件夾子文件夾和文件

[英]pysmb delete recursively folder sub-folder and files

我有一個共享文件夾,在根目錄下有一個帶有子文件夾和文件的文件夾。

我找不到如何用子文件夾和文件遞歸刪除根目錄下的文件夾的方法。

請給我一些幫助。

謝謝

這是Michael對於python 2.7的功能:

from smb.SMBConnection import SMBConnection

dry_run = True  # Set to True to test if all files/folders can be "walked". Set to False to perform the deletion.
userID = 'myuser'
password = 'mypassword'
client_machine_name = 'testclient'   # Usually safe to use 'testclient'
server_name = 'MYSERVER'   # Must match the NetBIOS name of the remote server
server_ip = '192.168.1.10' # Must point to the correct IP address
domain_name = ''           # Safe to leave blank, or fill in the domain used for your remote server
shared_folder = 'smbtest'  # Set to the shared folder name

conn = SMBConnection(userID, password, client_machine_name, server_name, domain=domain_name, use_ntlm_v2=True, is_direct_tcp=True)
conn.connect(server_ip, 445)

def walk_path(path):
    print 'Walking path', path
    for p in conn.listPath(shared_folder, path):
        if p.filename!='.' and p.filename!='..':
            parentPath = path
            if not parentPath.endswith('/'):
                parentPath += '/'

            if p.isDirectory:
                walk_path(parentPath+p.filename)
                print 'Deleting folder (%s) in %s' % ( p.filename, path )
                if not dry_run:
                    conn.deleteDirectory('smbtest', parentPath+p.filename)
            else:
                print 'Deleting file (%s) in %s' % ( p.filename, path )
                if not dry_run:
                    conn.deleteFiles('smbtest', parentPath+p.filename)

# Start and delete everything at shared folder root
walk_path('/')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM