Python Forum
Help | How to make Dynamic Path/Address - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help | How to make Dynamic Path/Address (/thread-19749.html)



Help | How to make Dynamic Path/Address - soothsayerpg - Jul-13-2019

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
os.chdir('supplemental_directory')
to make it easier but after Python reads that line, it would now be in the 'supplemental_directory', right?


RE: Help | How to make Dynamic Path/Address - SheeppOSU - Jul-13-2019

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')



RE: Help | How to make Dynamic Path/Address - Larz60+ - Jul-14-2019

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: CensusPaths.py
from pathlib import Path
import os


class CensusPaths:
    def __init__(self):
        # Anchor home path
        os.chdir(os.path.abspath(os.path.dirname(__file__)))

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

        self.datapath = self.rootpath / 'data'
        self.datapath.mkdir(exist_ok=True)

        self.jsonpath = self.datapath / 'json'
        self.jsonpath.mkdir(exist_ok=True)

        self.htmlpath = self.datapath / 'html'
        self.htmlpath.mkdir(exist_ok=True)

        self.tmppath = self.datapath / 'tmp'
        self.tmppath.mkdir(exist_ok=True)

        self.ftppath = self.datapath / 'ftp'
        self.ftppath.mkdir(exist_ok=True)
        
        self.controlpath = self.datapath / 'control'
        self.controlpath.mkdir(exist_ok=True)
        
        self.docspath = self.datapath / 'docs'
        self.docspath.mkdir(exist_ok=True)

        self.referencepath = self.docspath / 'reference'
        self.referencepath.mkdir(exist_ok=True)

if __name__ == '__main__':
    CensusPaths()
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 cityfile.open('w') as fp:
            json.dump(mydict, fp)



RE: Help | How to make Dynamic Path/Address - DeaD_EyE - Jul-14-2019

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.