cancel
Showing results for 
Search instead for 
Did you mean: 

Python script: Move

Sharpe-cory
New Contributor

Hello, I’m new to using Snaplogic and have gotten struck trying to integrate my python script into the script snap.

Purpose: Use python script in Script snap to move files and update the name with a timestamp.
Pipeline variables: fsrc, fdst, fname.

image

image

For my example I’m just trying to move a text file called: Test1.txt that is sitting at: sldb:///projects/Cory Sharpe to:sldb:///projects/Shared. While this can be done with a copy+delete snap, I’m looking to do this using a script. Thanks!

Code:

#Pseudo-code:
#Get: File Name, Source, and Destination pipeline variables
#Move: File name to Destination + append timestamp to File name

from com.snaplogic.scripting.language import ScriptHook
import os
import time

fsource = $_fsrc
fdest = $_fdst
fname= $_fname

#Dont think lists will work here
halved=
timeval=(time.strftime(“_%Y%m%d%H%M”))

class FTS(ScriptHook):
def init(self, input, output, error, log):
self.input = input
self.output = output
self.error = error
self.log = log
def execute(self):
try:
Result=FileCheck()
if Result:
print(fname)
SnapMove(fname)
else:
print(“Result == “,Result,”. Ending.”)
except Exception as e:
errWrapper = {
‘errMsg’ : str(e.args)
}
def SnapMove(fname):
try:
halved=fname.split(‘.’)
NewName=(halved[0]+timeval+“.”+halved[1])
fname = os.path.join(fsource, fname)
NewName = os.path.join(fdest, NewName)
os.rename(fname, NewName)
except Exception as e:
errWrapper = {
‘errMsg’ : str(e.args)
}

def FileCheck():
if os.path.exists(fdest)==False:
os.makedirs(fdest)
if os.path.exists(fsource)==False:
print(fsource, “does not exists.”)
return False
if os.path.isdir(fsource)==False:
return False
if os.path.exists(fsource+“/”+fname)==False:
return False
hook = FTS(input, output, error, log)

**note that tabs were lost on post.

4 REPLIES 4

akidave
Employee
Employee

The syntax error seems to be happening because running a Script through a file is not evaluating parameters. So $_param is not recognized, I will file a defect for that. One workaround is to put the script inline, in Edit Script, instead of in an external file. Another is to add a mapper snap before the script which will write the pipeline parameters to the document, the script can run from the file and get the parameters from the input doc.

The second issue is that the jython script cannot do a file rename on a SLFS file, since os.rename() method works on local files only. Since you want to move projects, use a file reader from source, write to destination project and then use the file delete snap to delete the source file.

Thank you for the reply, I will try both the inline and mapper snap workarounds.

Would it be possible to use the script snap to call these commands when working with files that are off of the SLFS?
Perhaps locally on my desktop or another server?

del
Contributor III

@Sharpe-cory, while @akidave is submitting a defect, you might try putting your variable assignments inside the "class FTS(ScriptHook):: to see if it eliminates the current error. I’ve successfully written several scripts and all of the script modifications are inside the class block.

Thanks, ill try moving the assignments into the class declaration area.