Python Forum
Help | How to make Dynamic Path/Address
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help | How to make Dynamic Path/Address
#1
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?
Reply
#2
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')
Reply
#3
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)
Reply
#4
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,203 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Looking for discord bot to make loop ping for address ip tinkode 0 1,823 Jul-26-2021, 03:51 PM
Last Post: tinkode
  I code a program to solve puzzle but i can't make it more dynamic. Shahmadhur13 5 2,730 Apr-18-2020, 10:05 AM
Last Post: Shahmadhur13
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 5,740 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020