Skip to main content

@takashi

Greetings FME sensei, good to see you're still around! I had a question about a Python script I'm using as a Private Parameter linked to two published parameters to generate log file folders that include the local user and date in the folder name, then generates time/date stamped log files within those folders.

Here's the script:

# Scripted (Python) Parameter Example

 

# Create log file path with time stamp.

 

import fme, time, os

 

d = FME_MacroValuesu'Log_File_Dir'] # directory path

 

w = FME_MacroValuesl'User_Name'] # workspace name

 

t = time.strftime('%Y%m%d%H%M%S') # time stamp

 

f = FME_MacroValuesV'Folder_Name_Date'] # date in folder name

 

# If the workspace name ends with ".fmw", remove it.

 

if w.endswith('.fmw'):

 

w = w<:-4]

 

 

# Create a new folder which has the name of the workspace.

 

dir = os.path.join(d, w, f)

 

if not os.path.exists(dir):

 

os.mkdir(dir)

 

 

# If the expected path conflicts with an existing file,

 

# append sequential number to avoid overwriting it.

 

base = os.path.join(dir, t)

 

path = '%s.log' % base

 

n = 1

 

while os.path.exists(path):

 

path = '%s_%d.log' % (base, n)

 

n += 1

 

return path

 

When I run the workbench with the text in bold set to the built-in FME_MF_DIR parameter, it runs correctly, but when I run it as above, it fails without returning any error notifications. What am I doing wrong?

 

The path defined by the "Log_File_Dir" parameter is a UNC network path. I tried a few combinations of the forward and backward slashes as indicated in prior posts, but to no avail.

 

Does the parent directory - $(Log_File_Dir)/$(User_Name) - exist? If not, try using os.makedirs() method instead of os.mkdir().

if not os.path.exists(dir):
    os.makedirs(dir) 

See these links to learn about the difference between those methods.

https://docs.python.org/3/library/os.html?highlight=mkdir#os.mkdir 

https://docs.python.org/3/library/os.html?highlight=makedirs#os.makedirs


Does the parent directory - $(Log_File_Dir)/$(User_Name) - exist? If not, try using os.makedirs() method instead of os.mkdir().

if not os.path.exists(dir):
    os.makedirs(dir) 

See these links to learn about the difference between those methods.

https://docs.python.org/3/library/os.html?highlight=mkdir#os.mkdir 

https://docs.python.org/3/library/os.html?highlight=makedirs#os.makedirs

Thanks Takashi! That worked like a charm. 

Now we have log file folders for each date FME is run, subfolders for each user (or the Remote Desktop calling batch files via Scheduled Tasks) that ran workbenches each day, and separate timestamped log files that include the name of the workbench for every instance that workbench was run. This is the exact tracking structure that I was pursuing.

Reply