0

I have created a Robot Framework custom keyword in a python script that returns the full file paths of files in a directory structure. I can run the python script separately and I get the expected results, however, when I use the script as a custom keyword the returned list in empty.

Any help is appreciated See below for code: ROBOT CODE

Settings 
Library  FilePaths.py

Test Cases

GetsubmissionFiles

  @{List}=  get filepaths  ////SomeServer//D//TestData//Automation//UK//

Here is the python code:

import os
class  FilePaths:
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'

d = "\\\\SomeServer\\D\\TestData\\Automation\\UK\\"

def get_filepaths(d):

    file_paths = []  
    for root, directories, files in os.walk(d):
        for filename in files:
            filepath = os.path.join(root, filename)
            file_paths.append(filepath)
    return file_paths

full_file_paths = get_filepaths(d)

print(full_file_paths)

Issue is Robot Framework results in an empty list value with none of the file paths Return value = []

4
  • 2
    Please fix the code formatting in the question. Is this the get_filepaths() definition? It's not a class method, but a function; and as there is a class with the same name as the file - Robotframework will import it (the class) and expose all its methods as keywords, and will not do the same for other functions in the file. Commented Dec 28, 2018 at 14:44
  • 1
    Why do you have so many slashes in the path? I'm pretty sure ////SomeServer//D//TestData//Automation//UK// is not a valid path. It's also not the same as the one you use in the python script (backslashes are very different from forward slashes) so it's wrong to conclude that it works when you call it from python but not when called from robot. Commented Dec 28, 2018 at 16:41
  • Looks like @BryanOakley nailed it - the target you want to get the file list of looks like an UNC ("a Windows share", SMB). Thus in your Robotframework code use the correct path character for it - a backward slash, e.g. this string - `\\\\SomeServer\\D\\TestData\\Automation\\UK\` Commented Dec 29, 2018 at 7:31
  • Thankyou very much both @bryanoakley and Todor Minakov I changed the path as suggested and it works as expected. See below for working keyword: import os class FilePaths: ROBOT_LIBRARY_SCOPE = 'GLOBAL' def get_filepath(self, d): file_paths = [] for root, directories, files in os.walk(d): for filename in files: # Join the two strings in order to form the full filepath. filepath = os.path.join(root, filename) file_paths.append(filepath) return file_paths Commented Jan 8, 2019 at 16:00

1 Answer 1

0

The problem is that you're not using a valid file path. For UNC paths you need to use just a single slash rather than a double slash:

@{List}=  get filepaths  //SomeServer/D/TestData/Automation/UK/

In your question you claim it "I can run the python script separately and I get the expected results", but in the python code you are using backslashes. Since you're using backslashes in a string, you must double them up. With forward slashes there is no need to do that.

In other words, it worked in python but not in robot because you were using a correct path in python and an incorrect path in robot.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.