Python Forum
How to import a file from outside the directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to import a file from outside the directory
#1
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.
Reply
#2
You need to add __init__.py files, see: https://timothybramlett.com/How_to_creat...t__py.html
Reply
#3
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'
Reply
#4
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))
Reply
#5
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'
Reply
#6
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,071 Jun-27-2023, 01:17 PM
Last Post: diver999
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,666 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Import XML file directly into Excel spreadsheet demdej 0 799 Jan-24-2023, 02:48 PM
Last Post: demdej
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 927 Jan-23-2023, 04:56 AM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,062 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  no such file or directory in SFTP saisankalpj 2 1,490 Nov-25-2022, 11:07 AM
Last Post: DeaD_EyE
Photo Making Zip file of a file and Directory Nasir 2 984 Oct-07-2022, 02:01 PM
Last Post: Nasir
  How to import another Python in different directory? dee 3 849 Sep-28-2022, 06:41 PM
Last Post: dee
  Failed to execute child process (No such file or directory) uriel 1 1,615 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  Need Help: FileNotFoundError:[Errno 2] No such file or directory python202209 5 2,523 Sep-12-2022, 04:50 AM
Last Post: python202209

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020