简体   繁体   English

python ftp方法-错误553无法创建文件

[英]python ftp method - error 553 Could not create file

i'm using the following def to upload files , the process is to check if directory exist or not if not it should create it then upload the file , 我正在使用以下def上传文件,过程是检查目录是否存在,如果不存在,则应创建该目录然后上传文件,

i keep getting the error , meanwhile if i connect with same user and passwd to the ftp i can create directories , i'm using vsftp as server 我不断收到错误消息,与此同时,如果我与同一用户连接并将passwd传递到ftp,我可以创建目录,我使用vsftp作为服务器

    def uploadFTP(filepath, filename_new, env):

    global config

    ftpsrv = config[env]["ftpsrv"]
    ftpusr = config[env]["ftpuser"]
    ftppwd = config[env]["ftppass"]

    filename = os.path.basename(filename_new)
    today = datetime.datetime.now()
    today_path = today.strftime("%Y/%m/%d")
    filename=os.path.join(today_path, filename)
    if not os.path.exists(os.path.join(os.path.dirname(filepath), today_path)):
                os.makedirs(os.path.join(os.path.dirname(filepath), today_path))
    try:
        ftp = ftplib.FTP(ftpsrv)
        ftp.login(ftpusr, ftppwd)
    except:
    logger.error("Ftp connection error has occurred")
        raise
    else:
        f = open(filepath, "r")
        cmd = "STOR %s" %(filename)
        out = ftp.storbinary(cmd, f)
        f.close()
        ftp.quit()
        return out

error as following : 错误如下:

 File "/usr/lib64/python2.6/ftplib.py", line 218, in getresp
    raise error_perm, resp
error_perm: 553 Could not create file.

Any advise here ? 有什么建议吗?

Update : 更新:

modified the function as following 修改功能如下

def uploadFTP(filepath, filename_new, env):

    global config

    ftpsrv = config[env]["ftpsrv"]
    ftpusr = config[env]["ftpuser"]
    ftppwd = config[env]["ftppass"]

    filename = os.path.basename(filename_new)
    today = datetime.datetime.now()
    today_path = today.strftime("%Y/%m/%d")
    filename=os.path.join(today_path, filename)
    if not os.path.exists(os.path.join(os.path.dirname(filepath), today_path)):
                os.makedirs(os.path.join(os.path.dirname(filepath), today_path))
    try:
        ftp = ftplib.FTP(ftpsrv)
        ftp.login(ftpusr, ftppwd)
    except:
        logger.error("Ftp connection error has occurred")
        raise
    else:
        f = open(filepath, "r")
        ftp.mkd(today_path)
        cmd = "STOR %s" %(filename)
        out = ftp.storbinary(cmd, f)
        f.close()
        ftp.quit()
        return out

and i'm getting 而我得到

   ftp.mkd(today_path)
  File "/usr/lib64/python2.6/ftplib.py", line 556, in mkd
    resp = self.sendcmd('MKD ' + dirname)
  File "/usr/lib64/python2.6/ftplib.py", line 243, in sendcmd
    return self.getresp()
  File "/usr/lib64/python2.6/ftplib.py", line 218, in getresp
    raise error_perm, resp
error_perm: 550 Create directory operation failed

note: permission in ftp folder is 777 and owner has full read and write , if i connect through ftp i can create folders but through this function i cant 注意:ftp文件夹中的权限为777,所有者拥有完全的读写权限,如果我通过ftp连接,则可以创建文件夹,但是通过此功能,我无法

advise please 请告知

You are passing a handle to a folder: 您正在将句柄传递到文件夹:

f = open(filepath, "r")

instead of a handle to a file: 而不是文件的句柄:

f = open(filepath + filename_new, "r")

UPDATE: 更新:

Your variable today_path has forward slashes and this suggests you want to create sub folders. 您的变量today_path具有正斜杠,这表明您要创建子文件夹。 You can't do this directly using ftp.mkd , however, you could use this solution from lecnt : 您不能使用ftp.mkd直接执行此ftp.mkd ,但是,可以从lecnt使用此解决方案

cdTree(today_path) cdTree(today_path)

Using this method from lecnt : lecnt使用此方法:

def cdTree(currentDir):
    if currentDir != "":
        try:
            ftp.cwd(currentDir)
        except IOError:
            cdTree("/".join(currentDir.split("/")[:-1]))
            ftp.mkd(currentDir)
            ftp.cwd(currentDir)

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

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