0

I have to use a config-file cfg.yml:

---
paths:
    reldir : ../my/dir

In Python, I run:

with open('cfg.yml', 'r') as config_file:
    cfg = yaml.load(config_file)

and my goal is to do something with some files in the directory reldir, via the Python file. The above works well.


However, this Python program must be able to run on Windows and Linux. If I am not mistaken, they use different path delimiters, / and \\. Thus, I want to make the reldir in the config file more robust:

---
paths:
    reldir : os.path.join('..','my','dir').

If my understanding is correct, this will combine these folder names with the correct delimiter, depending on where the Python program is executed.

However, this doesn't work, and print(reldir) outputs os.path.join('..','my','dir') instead of ../my/dir. I.e., it is taking the string literally without evaluating the os.path.join function.

I experimented with exec() and eval(), but first, I could not get it to run anyway; and second, I read here that I shouldn't use these.

How should I best proceed?

Marie. P.
  • 255
  • 1
  • 4
  • 17

1 Answers1

1

Usually "/" works for Windows as well. You can just try "../my/dir"

juha
  • 76
  • 6