Python Forum
How do I set the path to my module one directory below my python 3.7.3 program?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I set the path to my module one directory below my python 3.7.3 program?
#2
you can set the base path as follows
import os
from pathlib import Path
# Make sure in source path:
os.chdir(os.path.abspath(os.path.dirname(__file__)))
homepath = Path('.')
onebelow = homepath / 'onebelowname'
# Also, if you want path created if it doesn't already exist then add
onebelow.mkdir(exist_ok=True)
# to create a file in that directory:
mynewfile = onebelow / 'mynewfilename'
# To open that file for writing text
with mynewfile.open('w') as fp:
    fp.write('whatever')
Reply


Messages In This Thread
RE: How do I set the path to my module one directory below my python 3.7.3 program? - by Larz60+ - Jul-30-2019, 04:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  working directory if using windows path-variable chitarup 2 759 Nov-28-2023, 11:36 PM
Last Post: chitarup
  create a default path with idle to a specific directory greybill 0 890 Apr-23-2023, 04:32 AM
Last Post: greybill
  Import a module one step back of the path prathampatel9 1 1,092 Sep-21-2022, 01:34 PM
Last Post: snippsat
Question Setuptools CLI program ModuleNotFoundError after splitting up module into package Postbote 1 2,338 Nov-25-2021, 06:35 PM
Last Post: Postbote
  No Module found in other directory than source code [SOLVED] AlphaInc 1 2,090 Nov-10-2021, 04:34 PM
Last Post: AlphaInc
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,239 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  In put path and directory Led_Zeppelin 1 2,488 Apr-23-2021, 03:25 AM
Last Post: Larz60+
  OS module and file/directory recovery hikerguy62 1 2,185 Aug-02-2019, 06:42 AM
Last Post: Gribouillis
  Directory path gahhon 3 2,979 Mar-03-2019, 11:39 PM
Last Post: DeaD_EyE
  Directory path EOF Graham 8 4,711 Nov-28-2018, 10:13 PM
Last Post: Graham

Forum Jump:

User Panel Messages

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