Python Forum
How to import a file from outside the directory - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to import a file from outside the directory (/thread-10030.html)



How to import a file from outside the directory - sylas - May-09-2018

Hi all! I am in the directory kudoSudoku and in the file "solver.py". I want to import numbers.csv whose path is: Users\Sylas\numbers.csv. What do i write at the top of my solver.py file ? is it: "from Users.Sylas import numbers" ?? Thanks in advance.


RE: How to import a file from outside the directory - Larz60+ - May-09-2018

You need to add __init__.py files, see: https://timothybramlett.com/How_to_create_a_Python_Package_with___init__py.html


RE: How to import a file from outside the directory - sylas - May-10-2018

I added a __init__.py in my project. I still cannot import numbers . May I have please a reply without any link.

I just put "import numbers" at the top of my solvers.py file. Apparently this import is done, but I have another error message, because he looks for a file called "numbers.csv" which is not in my project.
Error:
λ python solver.py Traceback (most recent call last): File "solver.py", line 8, in <module> with open('numbers.csv', newline='') as csvfile: FileNotFoundError: [Errno 2] No such file or directory: 'numbers.csv'



RE: How to import a file from outside the directory - Larz60+ - May-10-2018

Quote:I want to import numbers.csv whose path is: ...
What threw me off is the word 'import' what you actually want to do is 'load' the file.
This code was written without testing, so may have minor errors
you can add the following statements in their appropriate places:
import os
import csv
...
filename = os.path.abspath('c:/users/Sylas/numbers.csv')
with open(filename) as csvfile:
    creader = csv.reader(csvfile, delimiter=' ')
    for row in creader:
        # do something with data 
        print(', '.join(row))



RE: How to import a file from outside the directory - sylas - May-10-2018

I give you my solver.py file, and then the error message.
#solver.py
import init
#import numbers
import os
import csv
from kudoSudoku import sudoku
from pprint import pprint
filename = os.path.abspath('C:/Users/Sylvain/numbers.csv')
with open(filename) as csvfile:
    creader = csv.reader(csvfile, delimiter=' ')
    for row in creader:
        #do something with data
        print(', '.join(row))


with open('numbers.csv', newline='') as csvfile:
    puzzle=csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in puzzle:#origin
        print(', '.join(row))#originr
        matrix=row

    for i in range(9):
        matrix[i]=eval(matrix[i])

    print('sylvain')
    print(matrix)
    pprint(matrix)

    table = sudoku(matrix)
    result = table.solve()
    pprint(result)
And now the error message:
Error:
λ python solver.py |[0,, 9,, 0,, 0,, 0,, 0,, 0,, 0,, 0]|, |[1,, 0,, 0,, 0,, 0,, 6,, 8,, 7,, 2]|, |[0,, 0,, 2,, 1,, 0,, 0,, 0,, 3, , 0]|, |[0,, 0,, 1,, 0,, 4,, 0,, 0,, 6,, 0]|, |[0,, 2,, 0,, 0,, 0,, 0,, 0,, 0,, 5]|, |[0,, 0,, 9,, 0,, 7,, 0,, 0,, 2,, 0]|, |[0,, 0,, 7,, 5,, 0,, 0,, 0,, 4,, 0]|, |[8,, 0,, 0,, 0,, 0,, 4,, 7,, 1,, 3]|, |[0,, 1,, 0,, 0,, 0,, 0,, 0,, 0,, 0]| Traceback (most recent call last): File "solver.py", line 16, in <module> with open('numbers.csv', newline='') as csvfile: FileNotFoundError: [Errno 2] No such file or directory: 'numbers.csv'



RE: How to import a file from outside the directory - Larz60+ - May-10-2018

The error is on line 16 ... Please make an effort to determine what is wrong ...
You are trying to open the file a second time!