Skip to main content
Solved

Problem With Python Parameter For Date/Time/User stamped log file folders/files


@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_MacroValues['Log_File_Dir'] # directory path

 

w = FME_MacroValues['User_Name'] # workspace name

 

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

 

f = FME_MacroValues['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.

 

Best answer by takashi

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

View original
Did this help you find an answer to your question?

2 replies

takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • Best Answer
  • January 25, 2020

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


takashi wrote:

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.

Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings