derh
derh

Reputation: 73

Excel Import - Error caused by filepath/filename

I have a problem with import from Excel. Filename is something like:

file_location = "R:\Projects\2-current\2015-06-02 data.xlsm"
book = xlrd.open_workbook(file_location)
sheet = book.sheet_by_index(1)

When I run it shows the following error:

OSError: [Errno 22] Invalid argument: "R:\Projects\x02 - current\x815-06-02 data.xlsm"

So there seems to be a problem with numbers as first character of a file/path (when I rename the file and put it directly into "R", everything works fine).

What can I do about it?

Upvotes: 1

Views: 341

Answers (1)

alec_djinn
alec_djinn

Reputation: 10799

Try file_location = "R:\\Projects\\2-current\\2015-06-02 data.xlsm" or file_location = r"R:\Projects\2-current\2015-06-02 data.xlsm"

The problem is that paths on windows require "\" that is a special char in python. You can solve similar issues using "...\\..." or r"...\...".

Lastly, importing the os module you can use file_location = os.path.normpath("R:/Projects/2-current/2015-06-02 data.xlsm") using the forward slash instead the back one.

Upvotes: 2

Related Questions