Python Forum
working directory if using windows path-variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
working directory if using windows path-variable
#1
Hello,

if have a Python-script in, let's say

d:\script\myScript.py

I add this to the windows path variable:

PATH="...;d:\script;..."

Then I open a shell at d:\working. When I invoke the script with

d:\working>myScript.py

how can I get the directory from which I invoke the script, so d:\working in this case.
Commands like os.getcwd() only returns the script directory d:\script

Any Ideas?

Thanks
Stephan
Reply
#2
When open a shell and run this program:
import os

print(os.getcwd())
It prints the name of path of the directory open in the shell, not the location of the program. For example, I open a CMD shell in "u:" and type
Output:
u:\>C:\Users\...\python\sandbox test.py u:\ u:\>
Can you post your program?
Reply
#3
Oh dear...

Yes, you are right. It's working this way.
I added

os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), './'))

so that it works out of visual studio code... Doh

Thank you!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  create a default path with idle to a specific directory greybill 0 881 Apr-23-2023, 04:32 AM
Last Post: greybill
  [SOLVED] [Windows] Right way to prompt for directory? Winfried 4 2,061 Jan-17-2023, 09:28 PM
Last Post: markoberk
  Pynput Library not Working with Windows jacknewport 1 2,361 Mar-26-2022, 07:09 PM
Last Post: snippsat
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,224 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  [solved] subdictionaries path as variable paul18fr 4 2,654 May-18-2021, 08:12 AM
Last Post: DeaD_EyE
  Subprocess.Popen() not working when reading file path from csv file herwin 13 15,123 May-07-2021, 03:26 PM
Last Post: herwin
  In put path and directory Led_Zeppelin 1 2,478 Apr-23-2021, 03:25 AM
Last Post: Larz60+
  Spyder stopped working in Windows 10 gammaray 3 3,094 Apr-19-2021, 05:33 PM
Last Post: jefsummers
  Setup working directory in IDLE Pavel_47 3 3,798 Mar-06-2021, 08:59 AM
Last Post: Pavel_47
  Does anyone have unicurses panel functions working on a Windows 10 platform? pjfarley3 0 1,473 Oct-11-2020, 04:41 AM
Last Post: pjfarley3

Forum Jump:

User Panel Messages

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