Python script: Move
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.
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.