Python - Path indication of a file for reading

1

I'm trying to establish the path to reading an excel table by xlrd .

But I get an answer saying that C: can not be used . How to correctly set the path of the files to be used?

import xlrd
workbook = xlrd.open_workbook("C:\Users\rdmsouza\Documents\python\programas\dados.xls")
workbook = xlrd.open_workbook("C:\Users\rdmsouza\Documents\python\programas\dados.xls", on_demand = True)
sheet.cell(0,0).value
  

OSError Traceback (most recent call   last) in ()         1 import xlrd   ---- > 2 workbook = xlrd.open_workbook ("C: \ Users \ rdmsouza \ Documents \ python \ programs \ data.xls")         3 workbook = xlrd.open_workbook ("C: \ Users \ rdmsouza \ Documents \ python \ programs \ data.xls",   on_demand = True)         4 sheet.cell (0,0) .value

     

C: \ Users \ rdmsouza \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ xlrd__init __. py   in open_workbook (filename, logfile, verbosity, use_mmap,   file_contents, encoding_override, formatting_info, on_demand,   ragged_rows)       393 peek = file_contents [: peeksz]       394 else:   - > 395 with open (filename, "rb") as f:       396 peek = f.read (peeksz)       397 if peek == b "PK \ x03 \ x04": # a ZIP file

     

OSError: [Errno 22] Invalid argument:   'C: \ Users \ rdmsouza \ Documents \ python \ programs \ data.xls'

    
asked by anonymous 11.08.2017 / 16:46

1 answer

1

Python uses the backslash to start a scape sequence, ie when the interpreter encounters a backslash, it expects the next character to be "special" or "control". So what you have to do is to inform that it is a normal character, putting the backslash itself first, ie, change all the single bars to double bars, like this:

workbook = xlrd.open_workbook("C:\Users\rdmsouza\Documents\python...

To complement this answer.

    
11.08.2017 / 19:42