FTP Server Error on ftpLib.py

Hi
I am currently using ignition version 8.1.14 and OS windows Server 2019.
I am trying to use the following script which is coming up with Error. I can’t find much information about the ftplib set.What should be the format for the File path or file name. I think script is error due to file path not in correct format.

Following is Script:

import ftplib
	
# Make sure to include double back slashes.  Should be a file path to a folder on the machine this script will run on.

filepath = "D:\\MGILL\WM_20220228_121611.xml"
	
# Check if report exists before trying to upload to FTP server.
file_exists = system.file.fileExists(filepath)
if (file_exists):
	# Connect and login to FTP site
	ftp = ftplib.FTP("ClientFTP")
	ftp.login("Username", "Password") # Username, Password
	ftp_path = "/" # Notice the forward slashes
	ftp.cwd(ftp_path)
	# Can use this to check you are in the proper folder (can comment out after debugging)
	print str(ftp.pwd())
	# Save the file to the FTP site
	ftp.storbinary("STOR " + filepath, open(filepath))
	# Quit the FTP session
	ftp.quit()

Error message is

Traceback (most recent call last):
  File "<event:actionPerformed>", line 22, in <module>
  File "C:\Users\Mg\.ignition\cache\gwlocalhost_8088\C1\pylib\ftplib.py", line 471, in storbinary
    conn = self.transfercmd(cmd, rest)
  File "C:\Users\Mg\.ignition\cache\gwlocalhost_8088\C1\pylib\ftplib.py", line 376, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "C:\Users\Mg\.ignition\cache\gwlocalhost_8088\C1\pylib\ftplib.py", line 339, in ntransfercmd
    resp = self.sendcmd(cmd)
  File "C:\Users\Mg\.ignition\cache\gwlocalhost_8088\C1\pylib\ftplib.py", line 249, in sendcmd
    return self.getresp()
  File "C:\Users\Mg\.ignition\cache\gwlocalhost_8088\C1\pylib\ftplib.py", line 224, in getresp
    raise error_perm, resp
ftplib.error_perm: 550 The parameter is incorrect. 


Ignition v8.1.14 (b2022012711)
Java: Azul Systems, Inc. 11.0.13

I took a look at the docs for ftplib and I’m wondering if you’re having problems trying to do STOR <local system filepath>. I’m betting that needs to be the path that you’re uploading to, not the source path. The fp object that you’re supplying is going to be the local file content.

Thanks kcollins for your quick reply.

If i understand you correctly according to Python documentation for FTP.storbinary.
Command should be STOR with FTP server folder Path?

There is no folder structure inside FTP as systems logon to FTP folder where this file has to be transferred to. I hope it makes sense.

Python documentation:
FTP. storbinary ( command , fp [, blocksize , callback , rest ])

Store a file in binary transfer mode. command should be an appropriate STOR command: "STOR filename" . fp is an open file object which is read until EOF using its read() method in blocks of size blocksize to provide the data to be stored. The blocksize argument defaults to 8192. callback is an optional single parameter callable that is called on each block of data after it is sent. rest means the same thing as in the transfercmd() method.

Changed in version 2.1: default for blocksize added.

Changed in version 2.6: callback parameter added.

Changed in version 2.7: rest parameter added.

In that case I’d try just using the base name of the file… The following might be useful to integrate into your script:

import os
filepath="C:\\path\\to\\file.txt"
print("Base name is '%s'" % os.path.basename(filepath))

The above, when run on Windows (and thusly understand the Windows-based file path separators, etc), will output file.txt as the base name, which you could use as the filename for the FTP remote (since you mention that you’re already going to be logged into the target path).

Thanks kcollins,
I have tried your suggestion, It didn’t work for me.
I did search few other things pointing to file path.
It appears that script is adding additional slash \ and complaining about path not found.

Noticed message is still displaying two slashes as one. Not sure why

Now i am getting error .
IOError: (2, ‘No such file or directory’, ‘D:\MGILL\Water\WM_20220228_121611.xml’)

It seems like there is an issue opening the file… Does that file exist in the scope (gateway or client/designer) that you’re running the script from? Note that if you’re running in the script console, it will be looking for the file on the computer you’re running the Designer from, not the gateway…

Currently i am running the script which is button action.
I am running client on the ignition gateway where this file is stored locally on D Drive.

Hi kcollins
I manage to solve the issue by removing the file from folder and just put into D drive. from there file transfer does work. I will later put the script code for other’s reference.

Hi Guys,
Later i noticed as pointed by others . If the script is running on gateway then you have to check if folder have premissions to read and write.

If the script is running on client with windows login which have premission to access all the folder with read and write then you won’t see the issue which is highlighted above. Final script which work on client is as follow. I still got same issue when run from gateway.

import ftplib
# Make sure to include double back slashes.
FTPfilepath = ('C:\\Folder\WM_'+ string2xmlFile + '.xml') #File path is concatenate with location and Datetime stamp
filename = ('WM_' + string2xmlFile + '.xml') # File name is concatenate name and Datetime stamp
file_object = open(os.path.realpath(FTPfilepath), 'rb')
# Check if report exists before trying to upload to FTP server.
file_exists = system.file.fileExists(FTPfilepath)
if (file_exists):
	# Connect and login to FTP site
	print('Connecting to  FTP Server')
	ftp = ftplib.FTP("client")
	ftp.login("username", "password") # Username, Password
	# Navigate to folder on the FTP site you want to save the file to.
	ftp_path = "/" # Notice the forward slashes
	ftp.cwd(ftp_path)
	# Can use this to check you are in the proper folder (can comment out after debugging)
	print str(ftp.pwd())
	# Save the file to the FTP site
	print('Storing File to  FTP server')
	ftp.storbinary('STOR ' + filename, file_object)
	print('File Stored to FTP server')
	# Close File Object
	file_object.close()
	# Quit the FTP session
	ftp.quit()

Can you edit and apply code formatting to your code? Select the text and hit the </> code formatting button. As it is all the indentation is lost. You’ll also get syntax highlighting for free! Thanks.