Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with JSON and Windows paths in Python?

Tags:

python

json

I'm trying to run a Python package which reads a JSON file (config file) for information.

The problem is I need to edit some of the JSON file to included directories.

The creator of the script was using a Linux kernel, so he used Linux pathnames, while I'm using Windows, which require backslashes within my pathnames.

This is causing a problem. Here is the how the JSON file looks:

"train": {
    "train_image_folder":   "C:/Users/Moondra/Desktop/Object Detection/basic-yolo-keras/train_image_folder",
    "train_annot_folder":   "C:/Users/Moondra/Desktop/Object Detection/basic-yolo-keras/train_annot_folder",     

    "train_times":          10,
    "pretrained_weights":   " ",
    "batch_size":           16,
    "learning_rate":        1e-4,
    "nb_epoch":             50,
    "warmup_epochs":        3,

    "object_scale":         5.0 ,
    "no_object_scale":      1.0,
    "coord_scale":          1.0,
    "class_scale":          1.0,

    "saved_weights_name":   "full_yolo_raccoon.h5",
    "debug":                true

},

"valid": {
    "valid_image_folder":   "C:/Users/Moondra/Desktop/Object Detection/basic-yolo-keras/valid_image_folder",
    "valid_annot_folder":   "C:/Users/Moondra/Desktop/Object Detection/basic-yolo-keras/valid_annot_folder",

    "valid_times": 1

}

I found this answer, but it wasn't really helpful in my case:

Remove Backslash from JSON string?

Is there an easy way around this?

like image 280
Moondra Avatar asked Jan 24 '18 06:01

Moondra


2 Answers

/ is a valid directory separator on Windows, and has been since MS-DOS 2. You can even mix them in the same path, but that looks horrible.

Some applications insist on \, but you might want to try using / before making work for yourself.

An important reason for using \ on Windows is where users expect it. The \ has got so ingrained in Windows culture that it unsettles people when they see /.

like image 166
cdarke Avatar answered Sep 21 '22 11:09

cdarke


Another way you may try. Read the desired path variable from your json file. And you can replace the path variable string / to \\. And hope it will work

# Assuming path variable string as "path"
windows_path = path.replace("/","\\")
like image 24
R.A.Munna Avatar answered Sep 18 '22 11:09

R.A.Munna