0

I am having a small issue that I cannot seem to solve. My program requires the user to input a path to a .csv file and then the program does stuff with it. Here is my code:

import pandas as pd

path = input("Please enter a path to a .csv file")
data = pd.read_csv(path)

I am running it in my terminal, so dragging the file into it yields what I believe to be the absolute path. The path looks like /Users/me/Downloads/sample.csv and the error message is FileNotFoundError: [Errno 2] File b'/Users/me/Downloads/sample.csv ' does not exist: b'/Users/me/Downloads/sample.csv '

I attempted to concatenate an r in front of it so that it would treat it as a raw string (that's what my google search yielded) but that just put r's in the path. So my question is where are these b's coming from before the path and how do I make this variable path work?

Jacob F
  • 135
  • 3
  • 13
  • 1
    Seems like there's some additional whitespace at the end of that file path. – Henry Ecker Jul 18 '21 at 23:06
  • It looks like it, but I'm just dragging the file into my terminal so i'm not sure why that would happen / be an issue – Jacob F Jul 18 '21 at 23:11
  • Why not `path.strip()`? You can't make a string raw after the fact when the program is running -- raw strings have to do with how the bytes are interpreted in the string literal as it appears in the source code. What OS are you using? I assume windows but it's good to be explicit. It's probably better to use an argument rather than an `input()` prompt for reasons like this, so a bit more justification as to why you need to use `input()` is helpful. – ggorlen Jul 18 '21 at 23:11
  • I don't understand how you can "drag a file into" an `input()` statement... – John Gordon Jul 18 '21 at 23:13
  • If you're dragging the file into your terminal on a mac it will add quotes around the path, try copying the path to the file by right clicking it and pasting that in. – 0x263A Jul 18 '21 at 23:19
  • path.strip() seems to work. – Jacob F Jul 19 '21 at 00:20

1 Answers1

0

To answer your question: the b prefix indicates that your path value is a byte literal, not a string.

Taking a look at the available Pandas documentation, it seems like read_csv expects a string value for the path. Although it may accept a byte literal value, it may not be able to handle it in an expected way.

This answer provides a good distinction between strings (sequences of characters) and byte literals (sequences of bytes), and this one demonstrates one way you could decode your byte-literal into a string value

donny-nyc
  • 21
  • 6
  • I tried to do path.decode() and it says AttributeError: 'str' object has no attribute 'decode' so I'm kind of even more confused now – Jacob F Jul 19 '21 at 00:14