Your problem is that you are escaping the closing quotes with that backslash \
when you do this:
r"C:\User\"
This means the string continues and that last "
just becomes part of the string. Meaning all this becomes one string:
r"C:\User\" + time + "
In addition, if you want the return value of your time
function to included in the path, you need to actually call it, meaning you need to do time()
, not just time
. The latter is just a reference to the callable/function, while the former actually calls it and returns a value.
To avoid most path problems I would suggest using pathlib
whenever possible. The Path
class takes care of correctly concatenating parts of a file path, automatically taking into account your operating system, and also allows you to do a whole bunch of other useful stuff with paths.
Here is how I would do it in your example:
...
path = Path("C:/User", time(), "SRMR_207228_1200326491_Portfolio_Margin_286165_1.xls")
xls = pd.ExcelFile(path)
...
PS:
Since there seem to be some misconceptions about how raw string literals work in Python, here is the relevant section in the documentation. An this is the relevant part:
The backslash (\
) character [...] can also be used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.
[...]
Even in a raw literal, quotes can be escaped with a backslash.
(Abbreviated and highlighted by me.)