Python Forum
PurePath.relative_to() fails in 3.6
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PurePath.relative_to() fails in 3.6
#3
I developed a stable path assignment a few years back.
The following file is always kept in the topmost source directory of a project, and symbolic links
are created in sub-source directories that point to this file.
Every module in the project imports this pathfile, thus allowing changes to be made in one place and immediately broadcast to every source file in the project.
I have never had an issue with relative files since I started using this method.
An additional benefit is that when a project is copied to a new computer, and I need to create the directory structure (with empty directories), I just run ProjectPaths.py and the directory structure is created
import os
from pathlib import Path

class ProjectPaths():
    '''
    This file contains paths used throughout the system.
    ** WARNING ** -- Never modify this file without keeping a backup.
                     Never modify this file if you're not absolutely sure how it works!
                     After modification: **TEST**, **TEST**, and **TEST AGAIN**
    '''
    def __init__(self):
        # Anchor Base Path -- Assures relative stability
        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)

# ========================== National ==========================

        self.national_datapath = self.datapath / 'National'
        self.national_datapath.mkdir(exist_ok=True)

        self.natlgeopath = self.national_datapath / 'Geo'
        self.natlgeopath.mkdir(exist_ok=True)

        self.natlhtmlpath = self.national_datapath / 'html'
        self.natlhtmlpath.mkdir(exist_ok=True)

        self.natlbackuppath = self.national_datapath / 'backup'
        self.natlbackuppath.mkdir(exist_ok=True)

        self.natlcsvpath = self.national_datapath / 'csv'
        self.natlcsvpath.mkdir(exist_ok=True)

        self.natldatabasepath = self.national_datapath / 'database'
        self.natldatabasepath.mkdir(exist_ok=True)

        self.natlexcelpath = self.national_datapath / 'excel'
        self.natlexcelpath.mkdir(exist_ok=True)

        self.natlhtmlpath = self.national_datapath / 'html'        
        self.natlhtmlpath.mkdir(exist_ok=True)

        self.natljsonpath = self.national_datapath / 'json'
        self.natljsonpath.mkdir(exist_ok=True)

        self.natllogpath = self.national_datapath / 'logs'
        self.natllogpath.mkdir(exist_ok=True)

        self.natlpdfpath = self.national_datapath / 'pdf'
        self.natlpdfpath.mkdir(exist_ok=True)

        self.natlprettypath = self.national_datapath / 'pretty'
        self.natlprettypath.mkdir(exist_ok=True)

        self.natlreportpath = self.national_datapath / 'reports'
        self.natlreportpath.mkdir(exist_ok=True)

        self.natltextpath = self.national_datapath / 'text'
        self.natltextpath.mkdir(exist_ok = True)

        self.natltmppath = self.national_datapath / 'tmp'
        self.natltmppath.mkdir(exist_ok=True)

if __name__ == '__main__':
    ProjectPaths()
Reply


Messages In This Thread
PurePath.relative_to() fails in 3.6 - by Skaperen - Sep-30-2019, 04:19 AM
RE: PurePath.relative_to() fails in 3.6 - by Larz60+ - Sep-30-2019, 07:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Path or PurePath Skaperen 2 8,003 Jul-06-2018, 02:06 AM
Last Post: Skaperen
  Path.relative_to() and symlinks Skaperen 0 2,892 Jun-07-2018, 03:19 AM
Last Post: Skaperen
  getting a full path string from a pathlib.PurePath object Skaperen 14 158,707 Mar-24-2018, 03:55 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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