lorenzo
lorenzo

Reputation: 21

problem with reading a csv file with pandas in executable

i'm writing a software that reads a csv file at after some steps creates another csv file as output, the software is working fine but when i try to create an executable with pyinstaller i have an error saying that my software can't find the input csv file. Here is how i am reading the csv file as input, i've also tryed to change the pathname with no luck:

import pandas as pd
def lettore(): 
  RawData = pd.read_csv('rawdata.csv', sep=';')
return RawData

how can i solve the problem?

Upvotes: 2

Views: 1079

Answers (1)

ionpoint
ionpoint

Reputation: 881

Your code searches for the file it the same folder where the exe is launched.

It is equivalent to

import os
import pandas 

filepath = os.path.join(os.getcwd(), 'filename.csv')
df = pd.read_csv(filepath)

Do not use relative paths when you create an exe.

I can give you two other options:

  1. Use an input to get the right file path when running the exe (or eventually use argparse).
filepath = input("insert your csv: ")
df = pd.read_csv(filepath)
  1. Define an absolute path and build it in your code (you cannot change it after building and the program will read the file only from that path).

Edit: after reading your comment, see also

How to reliably open a file in the same directory as a Python script

Upvotes: 1

Related Questions