You need a solid reference footing for your 'root' path
if you insert the statement below at the beginning of each script, it will set that footing
os.chdir(os.path.abspath(os.path.dirname(__file__)))
Now the starting point of each path is the same. and you can use relative.
pathlib is the new (object oriented) way to handle paths. Relative path problems go away.
see:
https://docs.python.org/3/library/pathlib.html
tutorial:
https://realpython.com/python-pathlib/#p...-of-a-path
I keep all of my path information in a separate file which I can import into each script. In this way, if I need to change the location of a file, or rename a directory I only have to change the path file, and the new location is immediately 'known' by all modules.
I also tend to describe paths node by node (example follows) this allows access to any part of the directory structure, by any module, at any time. I also allows me to run the path script one time in a new installation, and create a complete directory structure with tha one run.
I'm currently updating an old project that I have on GitHub, original here:
https://github.com/Larz60p/CaliforniaPublicSalaries
the new version will use pathlib, and although the following code is not complete, the pathlib portion is, and illustrates how easy it is to implement.
New (incomplete) code:
This is the path module that is imported into each of the other modules in the project.
Running this script for the first time, will create the directory structure if, (and only if) the directory doesn't already exist:
CaliforniaPaths.py
from pathlib import Path
import os
class CaliforniaPaths:
def __init__(self):
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.tmppath = self.datapath / 'tmp'
self.tmppath.mkdir(exist_ok=True)
self.htmlpath = self.datapath / 'html'
self.htmlpath.mkdir(exist_ok=True)
self.jsonpath = self.datapath / 'json'
self.jsonpath.mkdir(exist_ok=True)
if __name__ == '__main__':
CaliforniaPaths()
Incomplete Usage example:
# California compensation by city. Files are updated every working weekday
#
# Copyright (c) <2017 - 2019> <Larz60+>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Credits:
# Thanks to Snippsat for showing me how to scrape ASP.NET page.
#
# Author Larz60+
#
import CaliforniaPaths
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.filedialog as tfd
import tkinter.messagebox as tmb
import json
import getpass
import requests
from urllib.parse import urlparse
import UpdateCatalog
import socket
import os
import zipfile
class CaCompGui:
def __init__(self, parent=None, title='Tk'):
self.cpath = CaliforniaPaths.CaliforniaPaths()
if parent:
self.parent = parent
else:
self.parent = tk.Tk()
if socket.gethostbyname(socket.gethostname()) != '127.0.0.1':
self.s = ttk.Style()
self.s.theme_use('classic')
userid = getpass.getuser()
# print('userid: {}'.format(userid))
self.title = '{} -- {}'.format(title, userid)
catalog_url = 'http://publicpay.ca.gov/Reports/RawExport.aspx'
newcat = UpdateCatalog.UpdateCatalog()
newcat.build_json_data(catalog_url)
with open('data/CaCityCompensation.json') as f:
self.data = json.load(f)
self.parent.title(self.title)
self.parent_width = 1190
self.parent_height = 670
self.parent.geometry('{}x{}+10+10'.format(self.parent_width,
self.parent_height))
# GUI prototypes
self.fmain1 = tk.Frame
self.fmain2 = tk.Frame
self.frow = 0
self.tree = ttk.Treeview
self.download_tree = ttk.Treeview
self.textwin = tk.Text
self.txscroll = tk.Scrollbar
self.sb = tk.Frame
# tkinter variables
self.dest_dir = tk.StringVar()
self.dest_dir.set('Not defined')
self.url = tk.StringVar()
self.status = tk.StringVar()
self.treeheight_cat = 19
self.treeheight_download = 6
self.download_list = []
self.iid_list = []
self.build_gui()
self.status.set('Updating Catalog (changes daily)')
catalog_url = 'http://publicpay.ca.gov/Reports/RawExport.aspx'
newcat = UpdateCatalog.UpdateCatalog()
newcat.build_json_data(catalog_url)
self.status.set('Catalog is up to date')
with open('data/CaCityCompensation.json') as f:
self.data = json.load(f)
self.parent.mainloop()
else:
tmb.showerror('Internet', 'Please enable internet and restart')
def build_gui(self):
self.create_main_frames()
self.create_frame2()
self.create_frame3()
self.create_tree()
self.create_textwin()
self.create_statusbar()
def create_main_frames(self):
self.fmain1 = tk.Frame(self.parent, bd=2, relief=tk.RAISED)
self.fmain1.grid(row=self.frow, rowspan=self.treeheight_cat + 1,
column=0, sticky='nwew')
self.fmain2 = tk.Frame(self.parent, bd=2, relief=tk.RAISED)
self.fmain2.grid(row=self.frow, rowspan=self.treeheight_cat,
column=1, columnspan=3)
self.frow += self.treeheight_cat + 1
def create_frame2(self):
frame2 = tk.Frame(self.parent, bd=2, padx=2,
pady=2, relief=tk.RAISED)
frame2.grid(row=self.frow, rowspan=2, column=0,
columnspan=4, sticky='ew')
f2b1 = tk.Button(frame2, text='Choose',
command=self.get_dir)
f2b1.grid(row=0, column=0, sticky='ns')
f2l1 = tk.Label(frame2, bd=2, text='Destination Directory: ')
f2l1.grid(row=0, column=1, sticky='w')
f2l2 = tk.Label(frame2, textvar=self.dest_dir)
f2l2.grid(row=0, column=2, sticky='w')
self.frow += 2
def create_frame3(self):
frame3a = tk.Frame(self.parent, bd=2, padx=2,
pady=2, relief=tk.RAISED)
frame3a.grid(row=self.frow, rowspan=self.treeheight_download,
column=0, sticky='nsew')
frame3b = tk.Frame(self.parent, bd=2, padx=2,
pady=2, relief=tk.RAISED)
frame3b.grid(row=self.frow, rowspan=self.treeheight_download,
column=1, columnspan=4, sticky='nsew')
self.download_tree = ttk.Treeview(frame3b,
height=self.treeheight_download,
padding=(2, 2, 2, 2),
selectmode="extended")
self.download_tree.heading('#0', text='Files to download',
anchor=tk.CENTER)
self.download_tree.column('#0', stretch=tk.YES, width=400)
tree_down_scrolly = tk.Scrollbar(frame3b, orient=tk.VERTICAL,
command=self.download_tree.yview)
tree_down_scrolly.grid(row=0, rowspan=self.treeheight_download,
column=4, sticky='ns')
tree_down_scrollx = tk.Scrollbar(frame3b, orient=tk.HORIZONTAL,
command=self.download_tree.xview)
tree_down_scrollx.grid(row=self.treeheight_download + 1, column=0,
columnspan=4, sticky='ew')
self.download_tree.configure(yscroll=tree_down_scrolly)
self.download_tree.configure(xscroll=tree_down_scrollx)
self.download_tree.grid(row=0, rowspan=self.treeheight_download,
column=1, columnspan=3, sticky='nsew')
b1 = tk.Button(frame3a, text='Get Files', padx=2, pady=2, bd=2,
relief=tk.RAISED, command=self.download)
b1.grid(row=0, column=0, sticky='nsew')
b2 = tk.Button(frame3a, text='Exit', padx=2, pady=2, bd=2,
relief=tk.RAISED, command=self.quit)
b2.grid(row=2, column=0, sticky='nsew')
self.frow += self.treeheight_download + 1
def get_dir(self):
d = str(tfd.askdirectory())
print('d: {}'.format(d))
self.dest_dir.set(d)
def quit(self):
self.parent.destroy()
def create_tree(self):
treestyle = ttk.Style()
treestyle.configure('Treeview.Heading', foreground='white',
borderwidth=2, background='SteelBlue',
rowheight=self.treeheight_cat,
height=3)
self.tree = ttk.Treeview(self.fmain1,
height=self.treeheight_cat,
padding=(2, 2, 2, 2),
columns='Year',
selectmode="extended")
self.tree.heading('#0', text='Category', anchor=tk.CENTER)
self.tree.heading('#1', text='Year', anchor=tk.CENTER)
self.tree.column('#0', stretch=tk.YES, width=180)
self.tree.column('#1', stretch=tk.YES, width=50)
vatid = 1
for category, ignore in self.data['url_dict'].items():
if category == 'DataDictionary':
continue
cid = '{}'.format(vatid)
# print('cid: {}, category: {}'.format(cid, category))
self.tree.insert('', iid=cid, index='end',
text='{}'.format(category))
subid = 1
all_added = False
for year, url in self.data['url_dict'][category].items():
sid = '{}_{}'.format(cid, subid)
if not all_added:
self.tree.insert(cid, iid=sid, index='end',
text='{}'.format(category),
value='All')
all_added = True
else:
self.tree.insert(cid, iid=sid, index='end',
text='{}'.format(category),
value='{}'.format(year))
subid += 1
vatid += 1
self.tree.tag_configure('monospace', font='courier')
treescrolly = tk.Scrollbar(self.fmain1, orient=tk.VERTICAL,
command=self.tree.yview)
treescrolly.grid(row=0, rowspan=self.treeheight_cat, column=1, sticky='ns')
treescrollx = tk.Scrollbar(self.fmain1, orient=tk.HORIZONTAL,
command=self.tree.xview)
treescrollx.grid(row=self.treeheight_cat + 1, column=0, columnspan=2, sticky='ew')
self.tree.configure(yscroll=treescrolly)
self.tree.configure(xscroll=treescrollx)
self.tree.grid(row=0, rowspan=self.treeheight_cat, column=0, sticky='nsew')
self.tree.bind('<Double-1>', self.file_selected)
def create_textwin(self):
self.textwin = tk.Text(self.fmain2, bd=2, bg='#CEF6EC',
width=113, relief=tk.RAISED)
txscrolly = tk.Scrollbar(self.fmain2, orient=tk.VERTICAL,
command=self.textwin.yview)
txscrolly.grid(row=0, rowspan=self.treeheight_cat + 1, column=5, sticky='ns')
txscrollx = tk.Scrollbar(self.fmain2, orient=tk.HORIZONTAL,
command=self.textwin.xview)
txscrollx.grid(row=self.treeheight_cat + 2, column=3, columnspan=2, sticky='ew')
txscrollx.config(command=self.textwin.xview)
txscrolly.config(command=self.textwin.yview)
# self.textwin.configure(yscrollcommand=txscrolly.set)
# self.textwin.configure(xscrollcommand=txscrollx.set)
# self.textwin.configure(yscroll=txscrolly)
# self.textwin.configure(xscroll=txscrollx)
self.textwin.grid(row=0, rowspan=self.treeheight_cat + 1, column=3,
columnspan=2, padx=2, pady=2, sticky='nsew')
self.textwin.tag_configure('center', justify='center')
self.textwin.insert('end', 'Data Dictionary\n')
# self.textwin('center', 1.0, 'end')
for key, value in self.data['data_dict'].items():
# print('key: {}, value: {}'.format(key, value))
line = '\n{} -- {}\n'.format(key, value)
self.textwin.insert(tk.END, line)
self.textwin.insert(tk.END, '-------------------------------------------------------')
def create_statusbar(self):
self.sb = tk.Frame(self.parent, bd=2, padx=2,
pady=2)
self.sb.grid(row=self.frow, rowspan=2, column=0,
columnspan=4, sticky='nsew')
sbl1 = tk.Label(self.sb, bd=2, textvariable=self.status)
sbl1.grid(row=0, column=0, sticky='nsew')
def file_selected(self, event):
curitem = self.tree.focus()
cdict = self.tree.item(curitem)
category = cdict['text']
year = cdict['values'][0]
if year == 'All':
for nyear, url in self.data['url_dict'][category].items():
if url in self.download_list:
tmb.showerror('Select File', 'File already in list')
break
self.download_list.append(url)
self.download_tree.insert('', index='end', text='{}'.format(url))
else:
url = self.data['url_dict'][category][str(year)]
# print('category: {}, year: {}, url: {}'.format(category, year, url))
if url in self.download_list:
tmb.showerror('Select File', 'File already in list')
else:
self.download_list.append(url)
self.download_tree.insert('', index='end', text='{}'.format(url))
def download(self):
if self.dest_dir.get() == 'Not defined':
tmb.showerror('Download dir', 'Please specify download directory')
elif len(self.download_list) == 0:
tmb.showerror('Download files', 'Please select some files')
else:
for url in self.download_list:
urlsplit = urlparse(url)
basename = os.path.basename(urlsplit.path)
# print('urlsplit: {}'.format(urlsplit))
outfile = '{}/{}'.format(self.dest_dir.get(), basename)
# print('outfile: {}'.format(outfile))
self.status.set('downloading file: {}'.format(url))
with open(outfile, 'wb') as fo:
response = requests.get(url)
fo.write(response.content)
with zipfile.ZipFile(outfile) as z:
z.extractall(self.dest_dir.get())
os.remove(outfile)
self.status.set('Clearing download list: {}'.format(url))
self.download_list = []
for row in self.download_tree.get_children():
self.download_tree.delete(row)
self.status.set('Download Complete: {}'.format(url))
if __name__ == '__main__':
tt = CaCompGui(title='California City Compensation Datasets')
Note Line 45. This is how all of the paths become immediately available to any script.
now,
tmpdir = self.cpath.tmppath
tmpdir points directly to my tmppath directory
and I can define a file in that directory using:
newfile = tmpdir / 'newstuff.txt'
with newfile.open('w') as fp:
fp.write('Hello new file')
And now for the good part.
let's assume the following:
I want to create a new 'utility' directory between data and tmp
so that new tree will look like:
Output:
Old dir tree:
├── .
| └── data
| ├── tmp
| ├── html
| └── json
New dir tree:
├── .
| └── data
| ├── Utility
| ├── logs
| └── tmp
| ├── html
| └── json
First create the new utility node, and then move the tmp directory into that node
To do this, simply add a new node in 'CaliforniaPath.py' and modify old tmppath.
All scripts remain unchanged, and the new file structure is used immediately:
# Add a new node:
self.utilpath = self.datapath / 'utility'
self.utilpath.mkdir(exist_ok=True)
# modify tmppath to new location
self.tmppath = self.utilpath / 'tmp'
self.tmppath.mkdir(exist_ok=True)
That's it, all remain code now uses the new tmppath directory without any local code change.