0

For my program, I have 3 programs They will all be listed bellow

C:\Python\data\fold1\subfile.py

import json
import sys

part = sys.path[0].split('\\')                  '''This part adds
del (part[part.index('Python') + 1:len(part)])     a system path
sys.path.insert(1, '\\'.join(part))                C:\Python'''

from data.fold2.other import accounts_list

class AddUser():
    def __init__(self, username, password, Type=1):
        self.username = username
        self.password = password
        self.Type = Type
        accounts_list[self.username] = [self.password, self.Type]
        with open('data\\fold2\\other.py', 'w') as file:
            file.write('accounts_list = ' + json.dumps(accounts_list))

C:\Python\data\fold2\start.py

import sys
from other import accounts_list

part = sys.path[0].split('\\')                 '''This part adds
del (part[part.index('Python') + 1:len(part)])    a system path
sys.path.insert(1, '\\'.join(part))               C:\Python'''

from data.fold1 import subfile

subfile.AddUser('username', 'password')

C:\Python\data\fold2\other.py

accounts_list = {}

When I run my main program, start, I get the error:

  File "C:\Python\data\fold1\subfile.py", line 16, in __init__
    with open('data\\fold2\\other.py', 'w') as file:
FileNotFoundError: [Errno 2] No such file or directory:                 'data\\fold2\\other.py'

I have no idea how to resolve this error as I am still a beginner. If anyone knows what to do, please provide an answer.

-Thanks

Nic
  • 1,549
  • 2
  • 13
  • 13

1 Answers1

0

In terms of opening files I would recommend to take a look at: Open file in a relative location in Python Otherwise, your problem is simply as stated above that you are trying to access a nonexisting file. To switch between subfolders just use ../fold2/other.py.

Community
  • 1
  • 1
Philipp Braun
  • 1,583
  • 2
  • 25
  • 41
  • I know the directory is not usually in the sys.path so I added it in the code? And if the ... is C:\Python ect., I cant really add that as this program is on multiple different computers so it will be saved in different spots. – Nic May 08 '16 at 10:22
  • I also tried adding from ..data.fold2 ect. and get the error: SystemError: Parent module '' not loaded, cannot perform relative import – Nic May 08 '16 at 10:27
  • @Nic You are not changing you working directory. Do something similar to `os.chdir('/path/...')`. Though in my opinion it makes more sense to use relative dependencies. – Philipp Braun May 08 '16 at 10:27
  • What is relative dependencies? I am still a beginner at python. – Nic May 08 '16 at 10:29
  • From within a package, subdirectories are not included just like that; you need to "chain" the directories. (add a `__init__.py` file) In terms of relative dependencies just have a look at the link I posted. – Philipp Braun May 08 '16 at 10:33
  • Can you please give me some sort of an example with "chaining" the files and adding the init.py. That would be a real help! Thanks – Nic May 08 '16 at 10:35
  • Just include a `__init__.py` file in all your folders. https://docs.python.org/2/tutorial/modules.html#packages – Philipp Braun May 08 '16 at 10:39
  • Thanks! Ill figure it out. You saved my and my programs bacon :D Keep up the good work! – Nic May 08 '16 at 10:42