How to import CSV files from a same level folder (and/or above) in Python?

Asked

Viewed 94 times

0

I have already searched several topics here, unsuccessfully however. So, if there is already an equal question, please forgive me and indicate a solution. My project is articulated as follows:

linuxServer # /
----bin
----data
    ----arquivoCsv.csv
----packages
    ----lib
        ----module
            ----module.py
__init.py__ 
main.py

My idea is to use the module.py to call the fileCsv.csv. I can’t run the csv reader inside module.py. But if I test on main.py it works:

functional:

#linuxServer/main.py
import csv

somelist = []
with open('data/arquivoCsv.csv', 'r', encoding='ISO-8859-1')as csvFile:
    itens = csv.reader(csvFile, delimiter =';')
    for line in itens:
        somelist.append(line)

Nonfunctional:

#linuxServer/package/lib/module/module.py

import csv

somelist = []
with open('linuxServer/data/arquivoCsv.csv', 'r', encoding='ISO-8859-1')as csvFile:
    itens = csv.reader(csvFile, delimiter =';')
    for line in itens:
        somelist.append(line)

FileNotFoundError: [Errno 2] No such file or directory: 'linuxServer/data/arquivoCsv.csv'

PS: tips if the legal structure or need to change something will be welcome.

  • 1

    you tried to use '../data/arquivoCsv.csv' ?? In fact ../../../. But this would not be the best option. The modulo.py is imported by main.py ?

  • Yes, it gives the same error: Filenotfounderror: [Errno 2] No such file or directory: '.. /data/fileCsv.csv' Yes, the.py module is imported into main.py

  • If you are having difficulties in the file and are using a gnu/linux open the terminal in the folder csv and a pwd for it to show the path and paste in the csv reading direction.. Or inside the folder that the python files put the csv.. thus becoming open('arquivoCsv.csv')

  • The issue is that I don’t want to change any file from place. To on mac, hence pdw doesn’t work

1 answer

-1

In the main.py do something like:

import os

diretorio_base = os.path.dirname(os.path.abspath(__file__))  # ou use "." no lugar do `__file__`
diretorio_data = os.path.join(diretorio_base, "data")

When calling the function you are in modulo.py pass the diretorio_data as a parameter

Then just do it:

with open(os.path.join(PARAMETRO_RECEBIDO_PARA_DIRETORIO_DATA, 'arquivoCsv.csv'), 'r', encoding='ISO-8859-1')as csvFile:
  • Deu certo assim:
dir_base = os.path.dirname(os.path.abspath('./linuxServer'))
dir_data = os.path.join(diretorio_base, "data")

e dai lá no with open, em vez de usar a vírgula, usei o + pra concatenar. The only question is, whenever I need a file in other folders, will I need to pass the base folder and the one I want to access as parameter? There is no way to always access directly?

  • Can someone tell me why you voted negative so I can improve the answer ?

  • @Éricopiantkoski , imagine that your program can be installed by different people in different directories. The ideal is not to fix the place it will be installed. Using os.path, you will give the opportunity of this flexibility

  • Thanks a lot @Paulomarques helped a lot

Browser other questions tagged

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