0

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 ,

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

    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

advise please

Jecki
  • 802
  • 3
  • 16
  • 32

1 Answers1

0

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. You can't do this directly using ftp.mkd, however, you could use this solution from lecnt:

cdTree(today_path)

Using this method from lecnt:

def cdTree(currentDir):
    if currentDir != "":
        try:
            ftp.cwd(currentDir)
        except IOError:
            cdTree("/".join(currentDir.split("/")[:-1]))
            ftp.mkd(currentDir)
            ftp.cwd(currentDir)
Community
  • 1
  • 1
Alex
  • 21,273
  • 10
  • 61
  • 73
  • its return error IOError: [Errno 2] No such file or directory: , it should create the folder in remote location then place the file there .. its not doing it – Jecki Feb 09 '16 at 15:04
  • You need to call `ftp.mkd(filepath)` first if the directory doesn't exist – Alex Feb 09 '16 at 15:14
  • Your `today_path` contains forward slashes, do you want to create subfolders ? – Alex Feb 09 '16 at 20:22