Skip to main content
Solved

Using Python to delete files in a directory


fmelizard
Contributor
Forum|alt.badge.img+17
Hi Folks,

 

I want my workspace to clean up after itself. It creates a number of files in a temporary location, which is specified in a parameter: [FANOUT_DIRECTORY], which points to:

 

 

C:\\TempData\\translation_temp

 

 

Using FME 2014, I want to get python to delete all fines within this.

 

 

In theory, I need something like this:

 

 

#remove the text files

 

import os;

 

def FeatureProcessor(feature):

 

folder = str(FME_MacroValues['FANOUT_DIRECTORY'])

 

 

for the_file in os.listdir(folder):

 

  file_path = os.path.join(folder, the_file)

 

  try:

 

   if os.path.isfile(file_path):

 

    os.unlink(file_path)

 

  except Exception, e:

 

   print e

 

However, the value for "folder" when it gets the parameter FANOUT_DIRECTORY is:

 

 

C:<backslash>TempData<backslash>translation_temp

 

 

Which is obviously wrong. Anyone know how to get a parameter properly such that I can just use it? The examples I've seen don't appear to have this problem (but Python-FME documentation is sparse and conflicting).

 

 

Thanks.

Best answer by david_r

Btw, starting with FME 2014, you can also use the following method from the fmeobjects API:

 

 

folder = fmeobjects.FMESession().decodeFromFMEParsableText(FME_MacroValues['FANOUT_DIRECTORY'])

 

 

(If I'm not mistaken...)

 

 

David
View original
Did this help you find an answer to your question?
This post is closed to further activity.
It may be a question with a best answer, an implemented idea, or just a post needing no comment.
If you have a follow-up or related question, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

3 replies

david_r
Evangelist
  • August 11, 2014
Hi,

 

 

there are several solutions to this, but the easiest is perhaps:

 

 

folder = str(FME_MacroValues['FANOUT_DIRECTORY']).replace('<backslash>', '\\\\')

 

 

David

david_r
Evangelist
  • Best Answer
  • August 11, 2014
Btw, starting with FME 2014, you can also use the following method from the fmeobjects API:

 

 

folder = fmeobjects.FMESession().decodeFromFMEParsableText(FME_MacroValues['FANOUT_DIRECTORY'])

 

 

(If I'm not mistaken...)

 

 

David

fmelizard
Contributor
Forum|alt.badge.img+17
  • Author
  • Contributor
  • August 12, 2014
Hi David,

 

Thanks, the second one works like a charm!

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