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