Copy a file in Ignition

Hello,
How it’s possible copy a file from a path to another path with a script???

Thank you

As long as we are talking vision or the gateway scope, absolutely. You can use

import shutil

shutil.copyfile(src, dst)

# 2nd option
shutil.copy(src, dst)  # dst can be a folder; use shutil.copy2() to preserve timestamp

I use this in my application. Ignition uses jython 2.7 (or 2.5 if you are on 7.9 or earlier) so you can use stack overflow as well as long as you are looking for python 2.7/python 2.5 type answers. For instance I got the following answer I just provided you from here python - How to copy files? - Stack Overflow

There is also the system.file. group of functions that you could probably use as well to copy a file. I think you would use system.file.readAsBytes to get the data from your source file and then system.file.writeFile to write it to your destination.

Honestly you’re probably better off using the Ignition built-ins over jython implementations generally speaking. I still have the shutil verison in my code and it works fine but there’s always a chance of a poor jython implementation depending on your task so take my first paragraph about looking up python solutions with a grain of salt.

I try with this function, but we have this error:

Traceback (most recent call last):
File “event:mousePressed”, line 42, in
File “C:\Users\stefano.ignition\cache\gwlocalhost_8088\C2\pylib\shutil.py”, line 130, in copy2
copyfile(src, dst)
File “C:\Users\stefano.ignition\cache\gwlocalhost_8088\C2\pylib\shutil.py”, line 71, in copyfile
for fn in [src, dst]:
IOError: [Errno 13] Permission denied: ‘C:\x08ackup\daie.pdf’

This is the key part of the error. The Ignition user (i.e. the computer user account that's running Ignition) doesn't have permission to write to that directory.

2 Likes

Or backslashes weren’t escaped properly.

3 Likes

Ok, I write this path: ‘C:/backup\file.pdf’
Now it’s ok.

Thank you

1 Like