Skip to main content


Hi FME'ers


when creating a list with the following Python script:


import fme, os
import fmeobjects
folder = os.path.dirname(FME_MacroValuesM'Folder'])
def get_filepaths(feature):    """    This function will generate the file names in a directory     tree by walking the tree either top-down or bottom-up. For each     directory in the tree rooted at directory top (including top itself),     it yields a 3-tuple (dirpath, dirnames, filenames).    """
file_paths = Â]  # List which will store all of the full filepaths.    
# Walk the tree.
for root, directories, files in os.walk(folder):
        for filename in files:
            # Join the two strings in order to form the full filepath.
if '.dwg' in filename and '_georeferenced.dwg' not in filename:  
               filepath = os.path.join(root, filename)
               file_paths.append(filepath)  # Add it to the list.
feature.setAttribute('file_paths', file_paths)

I cannot easily retrieve the list of folders in FME.

I tried AttributeExposer with value 'file_paths{}', but it doesn't work

the AttributeExploder works and is a good workaround, but I'm curious how I could improve the script and expand my Python skills.  

Thanks in advance for your help.

best regards, 

Jelle

replace the feature.setAttribute with the following

for i, path in enumerate(file_paths):
   feature.setAttribute('file_paths{'+str(i)+'}', path)

Why are you trying to do this in python rather than using the Directory and File Pathnames reader?


Here's an even shorter (easier?) solution:

feature.setAttribute('file_paths{}', file_paths)

Notice the curly brackets after the list name.

David


There always is more than one way :-) You can also add list elements to the feature while finding target file names, without storing the file paths into a Python list.

    i = 0
    for root, directories, files in os.walk(folder):
        for filename in st for t in files if t.endswith('.dwg') and not t.endswith('_georeferenced.dwg')]:
            feature.setAttribute('file_paths{%d}' % i, os.path.join(root, filename))
            i += 1 

Scripting is fun, but I would firstly consider whether the PATH reader can be used, as @jdh mentioned at first.


Here's an even shorter (easier?) solution:

feature.setAttribute('file_paths{}', file_paths)

Notice the curly brackets after the list name.

David

I've never been able to get that to work with non string values (which I grant are not required for the OP).  

 

Python Exception <TypeError>: Failure to convert list values to native values.  

Whereas the loop  version has no issue.


I've never been able to get that to work with non string values (which I grant are not required for the OP).

 

Python Exception <TypeError>: Failure to convert list values to native values.

Whereas the loop version has no issue.

Ah, I didn't know that was an issue, I guess I've never encountered that situation. Thanks for the info.


Ah, I didn't know that was an issue, I guess I've never encountered that situation. Thanks for the info.

I mostly drop into python to do "list math", so encountered it very early on, and decided looping was a better way to go than type casting back and forth.


Why are you trying to do this in python rather than using the Directory and File Pathnames reader?

thank you , I wasn't aware of this reader. Very good to know!


Ah, I didn't know that was an issue, I guess I've never encountered that situation. Thanks for the info.

Hi David,

I remember trying this without success. It seemed obvious to me as well.


replace the feature.setAttribute with the following

for i, path in enumerate(file_paths):
   feature.setAttribute('file_paths{'+str(i)+'}', path)

Thank you Joanna. I was very close, but you taught me the tiny bit I needed additionally. I really appreciate it!


There always is more than one way :-) You can also add list elements to the feature while finding target file names, without storing the file paths into a Python list.

    i = 0
    for root, directories, files in os.walk(folder):
        for filename in st for t in files if t.endswith('.dwg') and not t.endswith('_georeferenced.dwg')]:
            feature.setAttribute('file_paths{%d}' % i, os.path.join(root, filename))
            i += 1 

Scripting is fun, but I would firstly consider whether the PATH reader can be used, as @jdh mentioned at first.

Thank you Takashi! Again, I learned something from you. 


Here's an even shorter (easier?) solution:

feature.setAttribute('file_paths{}', file_paths)

Notice the curly brackets after the list name.

David

Hi @david_r

 

I regularly use PythonCaller for dealing with lists , and I always get into trouble when I have a list that contains non-string items. I end up having to deal with them as strings within PythonCaller otherwise I get the same error as @jdh 'Python Exception : Failure to convert list values to native values.'

 

Do you know if there are any plans to fix this in future releases of FME (I am using FME 2017.1.1.1).

 

Thanks,

 


Hi @david_r

 

I regularly use PythonCaller for dealing with lists , and I always get into trouble when I have a list that contains non-string items. I end up having to deal with them as strings within PythonCaller otherwise I get the same error as @jdh 'Python Exception : Failure to convert list values to native values.'

 

Do you know if there are any plans to fix this in future releases of FME (I am using FME 2017.1.1.1).

 

Thanks,

 

Unfortunately I have no such insider information... :-(

 

If you need help finding a workaround, feel free to post it as a new questions, though. I'm sure someone has already done something on this subject.
Unfortunately I have no such insider information... :-(

 

If you need help finding a workaround, feel free to post it as a new questions, though. I'm sure someone has already done something on this subject.
No worries @david_r. I have a work around so it's not a major issue for me right now, but I have just posted an idea so Safe might consider changing it in future releases.

 

https://knowledge.safe.com/idea/67564/enable-pythoncaller-to-deal-with-lists-that-contai.html?

 


No worries @david_r. I have a work around so it's not a major issue for me right now, but I have just posted an idea so Safe might consider changing it in future releases.

 

https://knowledge.safe.com/idea/67564/enable-pythoncaller-to-deal-with-lists-that-contai.html?

 

Excellent, upvoted!

Reply