Ftplib下载zip文件尝试失败([Errno 13]权限被拒绝:'C:\\Users\\kbrab\\Desktop\\2019\\测试.zip)

2024-10-04 05:29:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试设置一个自动化的python脚本,将文件从远程FTP服务器下载到本地机器。我能够建立连接,导航到目录,但当试图下载特定的zip文件时,我得到一个错误。你知道吗

[Errno 13]权限被拒绝:“C:\Users\kbrab\Desktop\2019”\测试.zip'

我曾试过以管理员身份空闲运行,还检查了本地路径目录的创建和正确性。检查其他类似的职位,似乎是问题。FTP服务器是TLS/SSL隐式加密,python文件在windows虚拟机上运行。你知道吗

def checkKindred():
    time = a_day_in_previous_month()
    print(time)
    lines = []
    ftp_client.cwd('/kindred/')
    print("Current directory: " + ftp_client.pwd())
    ftp_client.retrlines('NLST',lines.append)
    nameCh = ("Attrition_"+str(time))
    for line in lines:
        if nameCh == line[:17]:
            print("found match")
            print(line)
            fileName = line
            unpackKindred(fileName,time)

def unpackKindred(name,time):
    local_path = "C:\\Users\\kbrab\\Desktop"
    local_path = os.path.join(local_path, str(time)[:4],"Attrition_2019-04-30.zip")
    if not os.path.exists(local_path):
        os.makedirs(local_path)
    try:
        filenames = ftp_client.nlst()
        ftp_client.retrbinary('RETR '+name, open(local_path, 'wb').write)      
    except Exception as e:
        print('Failed to download from ftp: '+ str(e))

代码现在通过martin的洞察工作,添加了以下更正的代码:

def unpackKindred(name,time):
    local_path = "C:\\Users\\kbrab\\Desktop"
    local_path = os.path.join(local_path, str(time)[:4])
    if not os.path.exists(local_path):
        os.makedirs(local_path)
    filename = os.path.join(local_path, name)
    file = open(filename, "wb")
    ftp_client.retrbinary("retr " + name, file.write)

Tags: 文件pathnameclienttimeoslocalline
2条回答

这将创建一个文件夹C:\Users\kbrab\Desktop\2019\test.zip

if not os.path.exists(local_path):
    os.makedirs(local_path)

这将尝试将文件夹视为一个文件:

ftp_client.retrbinary('RETR '+name, open(local_path, 'wb').write)      

检查文件夹的权限。设定好让每个人都能完全控制。你知道吗

相关问题 更多 >