0

My python config file looks like this:

[PATHS]
source = Q:\data\Energy\Source
archive = Q:\data\Energy\Archive

[DB]
host= localhost
user = root
passwd = *****
database= *****

Now I have a problem with my path, the location (actually the letter) changes every now and then because I'm making use of network drives. The network drives choses a location/letter based on its availability. So If X is not available for example, the network drive uses Y etc. The name of the directory and its subdirectory will never change, so they will always be called Energy\Source and Energy\Archive

How do I need to configure my paths to make sure they always get into the right location?

Mediterráneo
  • 115
  • 1
  • 9

1 Answers1

0

Building upon @tripleee suggestion, and assuming you have a way to know at runtime what the current drive letter is:

Create your config file like this (using basic interpolation)

[DEFAULT]
driveletter = X
[PATHS]
source = %(driveletter)s/data/Energy/Source
archive = %(driveletter)s/data/Energy/Archive

Read it, and change the default letter at runtime. Suppose you have a newletter variable set to "Z"

cfg = configparser.ConfigParser()
cfg.read("myconfig.ini")
cfg["DEFAULT"]["driveletter"] = newletter
cfg["PATHS"]["source"]
'Z/uat/data/Energy/Source'
gimix
  • 3,431
  • 2
  • 5
  • 21
  • My runtime is every 5 minutes, does that mean I have to change my config file every 5 minutes.... – Mediterráneo Sep 13 '21 at 10:34
  • I don't think your connection will change name every 5 minutes :) So your code just needs to check if the "old" connection letter is still valid, and if it's not then discover the current one (lots of Q/A here on that, for instance [this one](https://stackoverflow.com/questions/827371/is-there-a-way-to-list-all-the-available-windows-drives) ) and update the config file. No need for any manual update – gimix Sep 13 '21 at 12:21