Bottom Page

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?
Quote
#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')
Age Doesn't Matter, Your Passion Does
- Shreya Jain
Quote
#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)

Quote
#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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote

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)