繁体   English   中英

Python ftplib 删除文件夹和文件脚本(带隐藏文件)

[英]Python ftplib Delete Folder and Files Script (with hidden files)

我准备了一个简单的 python 脚本来删除所有目录和文件(带有隐藏文件)并分享给你。 您可以使用它或开发它。

总机 function:

# FTP
with FTP(hostname) as ftp:
    ftp.login(username, password)
    
    # delete all files
    delete(ftp, "public_html")
    ftp.cwd("public_html")

所需功能:

_ftp = None

def isFile(path):
    curDir = _ftp.pwd()
    try:
        _ftp.cwd(path)  # if we can cwd to it, it's a folder
        _ftp.cwd(curDir) # return current folder
        return False
    except ftplib.all_errors:
        return True

def isDirectory(path):
    curDir = _ftp.pwd()
    try:
        _ftp.cwd(path)  # if we can cwd to it, it's a folder
        _ftp.cwd(curDir) # return current folder
        return True
    except ftplib.all_errors:
        return False

def delete(ftp, path):
    global _ftp
    _ftp = ftp

    _ftp.cwd(path)
    files = _ftp.mlsd()

    for file, facts in files:  
        if file == "." or file == ".." or file == ".well-known": continue

        # if file, directly delete it
        if isFile(file):
            print("FILE deleting: " + file)
            _ftp.delete(file)
        
        if isDirectory(file):
            print("FOLDER deleting: " + file)
            try:
                _ftp.rmd(file)                
            except ftplib.all_errors:
                delete(_ftp, file)
                _ftp.rmd(file)
            
    _ftp.cwd("../")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM