2
import xlrd
book = xlrd.open_workbook("univ_list.xls")

I'm new to python. I'm trying to read a MS excel file that is in the same directory as my python script. Running the above code gives me a no such file or directory error.

I'll provide more information if needed.

Edit: code with full path upon request

import xlrd
book = xlrd.open_workbook("D:\Python_Scripts\univ_list.xls")

with corresponding error message

enter image description here

13
  • 1
    How are you running the script? Commented Jan 11, 2012 at 17:16
  • 4
    Just being in the same directory as the python script isn't enough; that directory also needs to be the current working directory. Try specifying the full path to your .xls; if that fixes it, then that was your problem. Commented Jan 11, 2012 at 17:17
  • @Mark I'm using the RUN button in PyScripter. Commented Jan 11, 2012 at 17:18
  • 1
    @TerryLiYifeng Can you add the new code you're trying? I don't run Windows, but IIRC you should use forward slashes even though you're on windows, since backslashes are escape characters in python strings. Commented Jan 11, 2012 at 17:28
  • 1
    Well, I'm not sure then. Just to sanity check, try import os.path; assert os.path.isfile(the_path). Commented Jan 11, 2012 at 17:36

5 Answers 5

2

You're getting hurt by string escapes. \ is an escape character for Python strings, so Python is attempting to find the \P and \u escape codes (among other things), which aren't going to be what you want.

The fix is either to escape the \ by changing the path to "D:\Python_Scripts\univ_list.xls", or to switch the string to an r"" (i.e. r"D:\Python_Scripts\univ_list.xls") string, which doesn't honor backslashes.

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

3 Comments

I don't quite think this is correct in this case. The backslash is the escape character, but it doesn't escape all characters. Using Raw Strings is good, but consider these examples: "\c" -> '\\c', "\b" -> '\x08', "\P" -> '\\P'. Try them in an interactive prompt. Python intelligently corrects the back slashes if the character following is not "special" when escaped.
@g.d.d.c Even so, it's good practice. What if he goes back later and wants to use the path to "D:\Python_scripts\new_workbook.xls"? The "\n" will be escaped.
@mlefavor - I agreed that Raw Strings are worthwhile, but this isn't the answer in this case, which is all I was pointing out. See the OP's comments, and the answer he submitted.
2

If python says you can't find a file, there are a couple steps you should take. The first is to make sure that the file exists. The first step is to make sure it's spelled correctly. Then, as suggested by AdamKG, make sure python can see it:

import os.path 
assert os.path.isfile(path_to_file)

Comments

1

just for supplement

Replaceing \ to / or \\ will be fine:

import xlrd
book = xlrd.open_workbook("D:/Python_Scripts/univ_list.xls")
 # book = xlrd.open_workbook("D:\\Python_Scripts\\univ_list.xls")
  • Because in Python strings, the backslash "\" is a special character, also called the "escape" character. You can read more in the document.

  • if you need \, actually you need \\.

Comments

0

The issue is that PyScripter sets the current directory. This is not the directory that your excel or python files is in (It will be probably be either your home, c:\ or the directory Pyscriper is in ( use os.getcwd() to get what it is).

Thus the fix is to provide the full path - but as shown in the other answers and comments this needs to be the string in raw form as Windows uses \ which do not mix well with the programming use of \ as an escape character in strings.

Comments

0
import xlrd
book = xlrd.open_workbook("univ_list.xls")

works perfectly well except that I need to replace xls with xlsx.

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.