Python - Path indication of a file for reading

Asked

Viewed 670 times

1

I’m trying to establish the way to read a table of excel for xlrd.

But I get an answer saying that the C: cannot be used. How to correctly define 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.xls data programs") 3 Workbook = xlrd.open_workbook("C: Users rdmsouza Documents python.xls data programs", 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 The following shall apply: --> 395 with open(filename, "Rb") as f: 396 Peek = f.read(peeksz) 397 if Peek == b"PK x03 X04": # a ZIP file

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

2 answers

1

Python uses the backslash for scape sequence start, i.e., when the interpreter finds a "backslash" he expects the next character to be "special", or "control". So what you have to do is inform that it is a normal character, putting the backslash itself before, that is, exchange all single bars for double bars, like this:

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

To complement see this answer.

  • Or, you can use the "/" forward as well - Python the interplay as a directory separator, even in Windows, precisely because of the duplicity of the inverted "".

  • @jsbueno True, but the problem is when vc depends on external variables that you have no control over, so you have to know the detail of the scape to necessarily make the conversion.

  • There are no "external variables" in this case - the "" only 'an escape character if it is in a string typed inside the Python source code. If someone types ma "" in an "input" entry, or if a "" is read from a text file, or from a database, it will always be a separate character, which acts as a separator. Experiment: do print(input()) and type a single "" or some of the escape combinations like n, t, etc...

  • Ok, I don’t have time and I don’t care to search now (even because my world is currently exclusively Linux), but... YES! in some contexts one can have HUGE problems (and I’ve had them). It’s no coincidence that there are libs to deal with this

  • But... I wanted to make a test, if not I went with configurations based on yaml files (python 2 something), but not.... I don’t think so! I won’t enter windows just for this. :-)

  • What I wrote is correct - is that YAML files can use Escaping with exactly the same syntax as Python - see: http://symfony.com/doc/current/components/yaml/yaml_format.html

  • Yes, and what I wrote is correct, I said "external variables", ie if you distribute an app that depends on a file yaml of which you have no direct participation, that is, built in the production environment by another professional team, you are dealing with a external variable, capisce? :-)

Show 2 more comments

1

You can also use an "r" at the beginning of a path, more or less like this:

workbook = xlrd.open_workbook(r"C:\\Users\rdmsouza\Documents\python\programas\dados.xls")

Then you will realize that every initial after the will be with a different color.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.