Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pytest and rootdirectory
#4
using os, I usually set my starting directory as the directory containing the script,
then set everything relative from there using pathlib.

the os command I issue during script initialization is:
os.chdir(os.path.abspath(os.path.dirname(__file__)))
then using pathlib, immediately after that:
# add at top with other imports:
from pathlib import Path
...
# somewhere in initialization:
homepath = Path('.')
base_path = homepath / '..' / 'data'
Once this is established, you can set relative paths from there
widgetpath = base_path / 'widgets'

zingo_widgetfile = widgetpath / 'zingo.dat'
with zingo_widgetfile.open() as fp:
    zingodata = fp.read()
etc.
Reply


Messages In This Thread
Pytest and rootdirectory - by Master_Sergius - May-31-2020, 06:02 PM
RE: Pytest and rootdirectory - by Larz60+ - Jun-01-2020, 12:24 AM
RE: Pytest and rootdirectory - by Master_Sergius - Jun-01-2020, 08:45 AM
RE: Pytest and rootdirectory - by Larz60+ - Jun-01-2020, 10:32 AM
RE: Pytest and rootdirectory - by Master_Sergius - Jun-01-2020, 05:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pytest Installed, but VS Code Won’t Access Pytest AstralWeeks 9 3,871 Sep-13-2023, 03:00 PM
Last Post: AstralWeeks
  Pytest mocks anthonyrmoss78 0 496 May-30-2023, 08:28 PM
Last Post: anthonyrmoss78
  Pytest with requests a21250450 3 2,886 Mar-21-2019, 03:24 PM
Last Post: buran
  pytest and caplog lazyliv 0 3,123 May-16-2018, 02:36 PM
Last Post: lazyliv

Forum Jump:

User Panel Messages

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