Detect file extension and save path

Hello everyone, i’m making a robot in wich one of the first task it’s to detect the extension to a file, wich can be either a xls or a csv, but in the end the process is the same: read the file as a table

The name can change but for tests i can apply a pattern to use with List Directory

Hi Sylvaranth,

While there are probably many ways to approach this I would suggest the easiest is to split off the file extension and then use something like Run Keyword If from the BuiltIn Library to conditionally run either Read Csv File or Read Xls File keywords you create.

as for splitting off the extension, well once you have the file path/file name in a variable thats quite easy you could use either:

So putting it all together:

  • get the filename
  • split off the extension from the filename into a variable you can use e.g. ${myext}
  • use Should Contain to check that the extension is one of the expected extensions (fail safe step) e.g
@{validext} =	Create List 	csv  	xls
Should Contain 	${validext} 	${myext}
  • create a Read Csv File that reads the csv file and returns the data the way you want it (dictionary object?)
  • create a Read Xls File that reads the excel file and returns the data the way you want it (dictionary object?)
  • use Run Keyword If to check if the extension is csv, if so call Read Csv File
  • use Run Keyword If to check if the extension is xls, if so call Read Xls File
  • Use the data returned

now you could then wrap those steps into another keyword called something like Read Data File and pass the file path to it, then you’d have a generic keyword you could reuse in many test cases.

Hope that helps,

Dave.

1 Like