-1

I'm new to python and I've not used os.path that often, but as my projects grew and I started trying to integrate multiple folders together, I realized that it is probably best that I start using the os.path method.

I'm currently trying to get to a json file data.json in a folder (that is within several other folders). I looked at the way it is done online but I think I confused myself.

import os 
import json

x = os.path.join('c:', 'data.json')
data = json.loads(open(x).read())
print(data)

Error message: FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Obada\\OneDrive\\Documents\\VS Code Projects\\Basic Chatbot\\data'

Obb-77
  • 44
  • 7
  • I found a way to use the raw string method, but I still feel like the os.path method might be better, not sure though. – Obb-77 May 23 '22 at 15:50
  • Can you post your "solution"? – Scott Hunter May 23 '22 at 15:52
  • @ScottHunter The raw string method is just using `r"fullPath"`, just an r before the string and `fullPath` just being the entire path to that file. For example mine turned out as such: `r"C:\Users\Obada\OneDrive\Documents\VS Code Projects\Basic Chatbot\intents.json"` . I didn't post it as a solution because it's different from the os.path method, which I'm still curious about – Obb-77 May 23 '22 at 15:57
  • the error message doesn't seem to match the code above, where does that long path come from? if you `print(x)` does it look like a valid path? – Anentropic May 23 '22 at 16:05
  • @Anentropic I just realized that it does not, this is the full path to the .json file `C:\Users\Obada\OneDrive\Documents\VS Code Projects\Basic Chatbot\data.json`, I think I had mixed up one of my attempts with this one, but I can see why the file couldn't be located on "`c:data.json`". I'm not sure how I can do it properly, do I add the inbetween for the path alongside the drive ( `('c:','\Users\Obada\OneDrive\Documents\VS Code Projects\Basic Chatbot\', 'data.json'` ) or do I have to add it in as another string between the drive and file? – Obb-77 May 23 '22 at 21:54
  • It's a long time since I used Windows, but it seems there's a couple of things happening: 1) `c:data.json` is understood as a relative path on the specified drive (weird!) https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#traditional-dos-paths and 2) for some reason Python os.path.join does not add separator when first segment is a drive letter, see further info here https://stackoverflow.com/questions/2422798/python-os-path-join-on-windows – Anentropic May 24 '22 at 08:47

3 Answers3

0

I would suggest using os.listdir() method for seeing the existing files in the currently active folder (you can use os.getcwd()) to see that.

import os 
import json

files = os.listdir()
x = os.path.join('c:', 'data.json')
for i in files:
    if x in files:
        data = json.loads(open(x).read())
        print(data)
    else:
        print("File doesn't exist!")

There are better ways to do this, but I am trying to keep it beginner-friendly as you are new to python.

outcast_dreamer
  • 143
  • 2
  • 7
0

The path.join() function is for programs that you want to run in diferent os, in this example you dont have to use the join function.

The join function just join the 2 strings with a / or \ depending on the os

In this example its windows so it use \

If its in the c partition you can just use the full (raw) address.

But if the address is relative like this example:

.\example.json

Its depend on the current file or folder that you are in, in this case the python script, you should use the join function

MJBN
  • 16
  • 1
  • 7
0

if you are on Windows, you can reach a file in C:\ by using something like this. Note that the second slash is because we need to tell python that it is a \

import os
fullfilename = os.path.join('C:\\', 'data.json')
data = json.loads(open(fullfilename).read())
print(data)

Another useful option is getcwd, which will give you the current directory python is running on.

currdir = os.getcwd()
FILEPATH = os.path.join(currdir,"data.json")

This will ensure that everything is working relative to the directory you are running your code from... no matter where you move the code

Redox
  • 9,321
  • 5
  • 9
  • 26