Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Help | How to make Dynamic Path/Address
I often see tutorials (mostly) demo of OS Module and every IO module with static addresses/path(s). But how about if we send our script to our friend, when he/she run the script, it won't work since "c:\\User\\"Isn't the same account\\" so the path breaks.

How can we create a dynamic path/address, were the script would identify the User's Account name and would not break the path. Do I also need to use RegEx? If so, some directory hierarchy isn't the same in count, and in-turn with also break the code again, right?

Also, what if I found a way with the said problem above, trying to use
to make it easier but after Python reads that line, it would now be in the 'supplemental_directory', right?
You can use os with pathlib
from pathlib import Path
import os

parent = Path(__file__).parent
car_pic = os.path.join(parent, 'Data', 'Pics', 'Car.png')
Age Doesn't Matter, Your Passion Does
- Shreya Jain
I, for all projects, keep one path file, which will contain all paths and path related objects (like database locations, etc.
This structure, using pathlib, will create the entire path tree (if it doesn't yet esist) first time run.
If you ever move your project, the relative file structure will move with you.
small example:
from pathlib import Path
import os

class CensusPaths:
    def __init__(self):
        # Anchor home path

        self.homepath = Path('.')
        self.rootpath = self.homepath / '..'

        self.datapath = self.rootpath / 'data'

        self.jsonpath = self.datapath / 'json'

        self.htmlpath = self.datapath / 'html'

        self.tmppath = self.datapath / 'tmp'

        self.ftppath = self.datapath / 'ftp'
        self.controlpath = self.datapath / 'control'
        self.docspath = self.datapath / 'docs'

        self.referencepath = self.docspath / 'reference'

if __name__ == '__main__':

Now to create a json file which will end up in the json directory (from another script:

import CensusPaths

class myclass:
    def __init__(self):
        self.cpath = CensusPaths.CensusPaths()


    def my_json_stuff(self):
        mydict = {
        cityfile = self.cpath / 'cityfile.json'

        with'w') as fp:
            json.dump(mydict, fp)

Path has the ability to used like os.path.join:

from pathlib import Path

parent = Path(__file__).parent
car_pic = Path(parent, 'Data', 'Pics', 'Car.png')
Also os.path.absolute is not needed:

parent = Path(__file__).parent.absolute()
Functions/Methods were added since Python 3.5. I use it with Python 3.6, but I can't memorize the missing functions/methods in older versions.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 1,113 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908

Forum Jump:

Users browsing this thread: 1 Guest(s)