0
dir_path = os.path.dirname(os.path.realpath(__file__))
from os.path import isfile, join
onlyfiles = [f for f in listdir(dir_path) if isfile(join(dir_path, f))]

print(onlyfiles);

with open("config.json", 'r') as jsondata:
    config = json.loads(jsondata.read())

Running this code, somehow, triggers a non existing error despite the file being listed during

print(onlyfiles);

Here is the full output log from the console.

Traceback (most recent call last):
['builder.py', 'builder.spec', 'builderloader2.rb', 'config.json', 
'Run.bat', 'Run.bat.lnk', 'test.json']

  File "C:/Users/cody.jones/Desktop/Builder Generator Release/builder.py", 
line 26, in <module>
    with open("config.json", 'r') as jsondata:

FileNotFoundError: [Errno 2] No such file or directory: 'config.json'

Process finished with exit code 1
Mahesh Karia
  • 2,045
  • 1
  • 12
  • 23
Cody
  • 149
  • 1
  • 10
  • Possible duplicate of [File Not Found Error in Python](https://stackoverflow.com/questions/17658856/file-not-found-error-in-python) – tripleee Dec 20 '17 at 05:45

2 Answers2

1

provide full path to open() instead of just file name as by default it will look for file in same directory

try:

open(r"C:/Users/cody.jones/Desktop/Builder Generator Release/config.json", "r")
Mahesh Karia
  • 2,045
  • 1
  • 12
  • 23
-1

The script will look for config.json in the current working directory - which presumably is not the same as the folder that the script resides in.

Update your open call to include the path you've already generated.

with open(os.path.join(dir_path, "config.json"), 'r')) as jsondata:

By doing it this way (rather than just including the absolute path) this script will still work if you move it to a different directory or computer so long as you keep the script and config together.

Shadow
  • 8,749
  • 4
  • 47
  • 57