File system paths
About file paths
Linux and Mac file paths use forward slash /
as path separator (eg. /usr/bin/my_file.txt
). Windows uses the backward slash \
(eg. C:\users\some_user_name\my_file.txt
). In non-Windows environments the backward slash \
generally works as an escape character => the character following the \
is escaped => the following character is so to say ignored thus C:\full\path\to\my_file.txt
results to C:ullathoy_file.txt
on these operating systems.
Robot Framework file paths
Robot Framework treats \
as an escape character on any operating system! Keep this in mind when you get 'File not found' error messages as also me myself made many times the mistake to accidentally use \
instead of /
. Thus, to define a Windows file path you need to escape the escape character -> use \\
:
*** Variables ***
${MY_WINDOWS_ABSOLUTE_PATH} C:\\temp\\my_file.txt # path to my_file.txt in C:\temp directory
Robot Framework has a built-in variable: ${/}
which is auto-converted to the path separator used by the host operating system, thus it is advised to always use this when dealing with file paths:
*** Variables ***
${MY_RELATIVE_PATH} ..${/}some${/}relative${/}path${/}my_file.txt
${MY_LINUX_ABSOLUTE_PATH} ${/}usr${/}bin${/}my_file.txt
${MY_WINDOWS_ABSOLUTE_PATH} C:${/}temp${/}my_file.txt
Beware of ${\}
instead of ${/}
! This escapes the ending }
and you get a weird error.
Useful path handling keywords
If you want to create new paths, especially if the individual path parts are a result of other keywords where there may be unexpected slashes at the beginning and at the end, you may use the OperatingSystem
library's keyword Join Path
. The advantage is that the resulting path is normalized (eventual duplicate slashes and dots are removed) and you get a valid file path for the host operating system:
*** Settings ***
Library OperatingSystem
*** Variables ***
${ROOT_DIR} root_dir${/} # notice the extra slash at the end
${MY_DIR_PATH} ${ROOT_DIR}${/}subdir # results to root_dir//subdir => invalid path!
*** Tasks ***
My Task
# on Windows the path separators are printed as \ and on Linux as /
# removes duplicate slashes
${my_new_path}= Join Path ${MY_DIR_PATH} another_dir another_file.txt
Log To Console ${my_new_path} # root_dir\subdir\another_dir\another_file.txt
or just validate your resulting path via the Normalize Path keyword:
*** Settings ***
Library OperatingSystem
*** Variables ***
# OperatingSystem lib keywords autoconvert / to \ on Windows
${MALFORMED_PATH} dir//////subdir/./file.txt
*** Tasks ***
My Task
# on Windows the path separators are printed as \ and on Linux as /
${valid_path}= Normalize Path ${MALFORMED_PATH}
Log To Console ${valid_path} # dir\subdir\file.txt
To get the file name from a path or just the extension:
*** Settings ***
Library OperatingSystem
*** Tasks ***
My Task
# on Windows the path separators are printed as \ and on Linux as /
${dir_path} ${file}= Split Path root_dir${/}some_dir${/}my_file.txt
Log To Console ${dir_path} # root_dir\some_dir
Log To Console ${file} # my_file.txt
${file_name} ${ext}= Split Extension ${file}
Log To Console ${file_name} # my_file
Log To Console ${ext} # txt
Related
- TODO importing libraries, resource and variable files