-1

I'm trying to get familiar with xlrd so I copied an example into my IDE (spyder). I'm using python(x,y) 2.7.6.1

This is my example

import xlrd
import os

filename=os.path.join("C:/","Desktop/myfile"):
book = xlrd.open_workbook(filename)
print "The number of worksheets is", book.nsheets
print "Worksheet name(s):", book.sheet_names()
sh = book.sheet_by_index(0)
print sh.name, sh.nrows, sh.ncols
print "Cell D30 is", sh.cell_value(rowx=29, colx=3)
for rx in range(sh.nrows):
    print sh.row(rx)

As you can see, I listened to advice on SE here but it still does not work (syntax error). As it is advised here I have written stuff in os.path.join() in manner given in the accepted answer.

This is error log:

runfile('C:/Users/PC/.spyder2/.temp.py', wdir=r'C:/Users/PC/.spyder2')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
  File "C:/Users/PC/.spyder2/.temp.py", line 12
    filename=os.path.join("C:/","/Users/PC/Desktop/myfile"):
                                                      ^

SyntaxError: invalid syntax

UPDATE Now, when I removed colon from the end of the line with "join" i got another syntax error. This is it:

runfile('C:/Users/PC/.spyder2/.temp.py', wdir=r'C:/Users/PC/.spyder2')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
 File "C:/Users/PC/.spyder2/.temp.py", line 13, in <module>
    book = xlrd.open_workbook(filename)
  File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 394, in open_workbook
    f = open(filename, "rb")
IOError: [Errno 2] No such file or directory: 'C:/Users/PC/Desktop/myfile'

What am I doing wrong? What should I do instead?

Community
  • 1
  • 1
user46147
  • 232
  • 1
  • 16

3 Answers3

1

The syntax error is that there should be no : at the end of the first line.

The "no such file or directory" error is because the desktop is not a directory located at "C:/Desktop". There's actually more than one directory whose contents show on the desktop, but probably what you want is "C:/Users/USERNAME/Desktop/", where USERNAME is of course your username on the machine.

If you want to access the home directory in general (i.e. not just yours, you want the home directory of whoever is running the script) then you can access the HOMEDRIVE and HOMEPATH environment variables.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • Now, my line with join() looks like this: filename=os.path.join("C:/","/Users/PC/Desktop/myfile"): And still I get syntax error. – user46147 Nov 29 '14 at 23:07
  • 1
    You don't need the join at all.. Just make it filename="C:/Users/USERNAME/Desktop/myfile.xls" # or whatever – demented hedgehog Nov 29 '14 at 23:08
  • @user46147: oh, sorry, I thought the error you're getting is as in the title "no such directory". The syntax error is that there shouldn't be a colon at the end of the line, I'll add that to my answer. I didn't spot that at first. – Steve Jessop Nov 29 '14 at 23:09
  • Isn't that what I said? – demented hedgehog Nov 29 '14 at 23:42
  • @dementedhedgehog: yes, and you posted your answer concerning the colon 7 seconds before I posted my edit concerning the colon to my answer. I wish I could type fast enough to have plagiarised you. – Steve Jessop Nov 29 '14 at 23:47
  • No worries.. I've done that too. There's a race condition with answering :) – demented hedgehog Nov 29 '14 at 23:48
1

It's the colon at the end of the join line. It shouldn't be there.

demented hedgehog
  • 7,007
  • 4
  • 42
  • 49
  • You're right. I removed the colon and now there is no mentioned syntax error. Now I get another syntax error. (updating my question) – user46147 Nov 29 '14 at 23:15
  • That one tells you what the problem is. Your filename is not correct. There's no such file. (are you missing the .xls extension?) – demented hedgehog Nov 29 '14 at 23:31
0

As suggested by demented hedgehog and Steve Jessop i had to convert following line

filename=os.path.join("C:/","Desktop/myfile"):

into

filename=os.path.join("C:/","/Users/PC/Desktop/myfile.xls")

So I just had to remove colon, write correct directory and write myfile.xls instead of just myfile.

user46147
  • 232
  • 1
  • 16