0

I am looking to use datatable in Python to read in a csv using

df = dt.fread(r"C:\Users\Tom\Documents\Python\project 1\Data\train_2017.csv")

However, I am getting the error:

Unable to obtain size of C:\Users\Tony\Documents\Python\project 1\Data\train_2017.csv: [errno 2] No such file or directory

I am only able to import using the below alternative method:

import os
os.chdir('c:\\Users\\Tom\\Documents\\Python\\project 1\\Data')
train = dt.fread("train_2017.csv")

but the above is not practical as I have csvs outside of the project folder. In R's data.table I would do:

fread(file.choose())

Does datatable in Python have the equivalent or what is the correct way of explicitly spelling out a csv's directory which is different to the working directory? Thank you

r2evans
  • 141,215
  • 6
  • 77
  • 149
Sweepy Dodo
  • 1,761
  • 9
  • 15
  • 1
    I found out I had to wrap the directory in `open` before passing to `dt.fread` i.e. `df = dt.fread(open(r"C:\Users\Tom\Documents\Python\project 1\Data\train_2017.csv"))` However, does Python have a way to prompt a browse Window to select csv like R's `fread(file.choose())` ? – Sweepy Dodo Aug 14 '21 at 16:13
  • 1
    1. You should post your findings as an answer. 2. For the fileopen dialog, you can try Tkinter's `filedialog.askopenfilename()` [based on this answer](https://stackoverflow.com/a/14119223/1431750). 3. You would still need to use `dt.fread(open(user_provided_filename))`. – aneroid Aug 14 '21 at 16:53
  • 1
    @aneroid wow that is neat `dt.fread(open(filedialog.askopenfilename()))` Thank you – Sweepy Dodo Aug 14 '21 at 17:18
  • FYI, I edited the tags for this question, the [tag:data.table] tag is specific to the R programming language. – r2evans Aug 14 '21 at 17:24
  • 1
    @r2evans Understandable. Though I only opted for `data.table` as `datatable` seemed too ambiguous as the latter is still rather new in the Python community – Sweepy Dodo Aug 14 '21 at 17:26
  • 1
    That's fair ... though it is a little "off" for R users (who aren't also python users) to come to a tag that is R-specific but not asking an R question (just R for context). If you'd like, I can reverse my edit (or you can), I'm not hard-over on it, just trying to help (and "clean up" some tag pollution ... usually it's the Stack tag-recommendation engine that suggests slightly-off tags). – r2evans Aug 14 '21 at 17:29
  • Not at all. I'm fine with it too. I only wish Matt Dowle called it data.table :) – Sweepy Dodo Aug 14 '21 at 17:31

1 Answers1

0

I found out I had to wrap the directory in open before passing to dt.fread i.e. df = dt.fread(open(r"C:\Users\Tom\Documents\Python\project 1\Data\train_2017.csv"))

Sweepy Dodo
  • 1,761
  • 9
  • 15