2

I want to execute a python script from a certain directory and want to change the working directory for the execution without cd-ing in the shell or script. Is there a way to do this with the regular Python 3 interpreter?

Say I'm in the directory cwd and there is a script baz.py in a sub(sub)directory that I want to execute.

cwd/foo/bar/baz.py

Is it possible to tell the interpreter to use any directory among cwd, foo, bar (or anywhere else in the filesystem) as the working directory where the script is executed? IDEs can do this in run configurations, what's the simplest way to achieve it without actually cd-ing into the other directory?

EDIT: I know how to change the directory from within the script, but I'm looking for a way to tell the interpreter where to execute it without modifying the script itself.

martineau
  • 119,623
  • 25
  • 170
  • 301
kochsalz
  • 21
  • 2
  • I don't find your question is 100% clear but I assume you're writing/opening files in which case you set the full path, for example, if you were opening a file `file.txt`, you could use the command: `file = open(r"C\Users\Kochsalz\cwd")`, or whatever your full path is. – GAP2002 Feb 10 '21 at 17:53
  • Too bad you never got an answer to this. The "duplicate" marking is silly. Guess what we both want is not possible... – AlexGeorg Mar 15 '22 at 09:58

2 Answers2

1
# Import the os module
import os

# Print the current working directory
print("Current working directory: {0}".format(os.getcwd()))


# Change the current working directory
os.chdir('/tmp')

# Print the current working directory
print("Current working directory: {0}".format(os.getcwd()))

copied from https://linuxize.com/post/python-get-change-current-working-directory/

I would say use : os.chdir

from Python Documentation https://docs.python.org/3/library/os.html:

os.chdir(path)

Change the current working directory to path.

This function can support specifying a file descriptor. The descriptor must refer to an opened directory, not an open file.

This function can raise OSError and subclasses such as FileNotFoundError, PermissionError, and NotADirectoryError.

Raises an auditing event os.chdir with argument path.

New in version 3.3: Added support for specifying path as a file descriptor on some platforms.

Changed in version 3.6: Accepts a path-like object.
pippo1980
  • 2,181
  • 3
  • 14
  • 30
  • thanks for the answer, but I was specifically looking for a way that doesn't edit the script, as I said in my question. – kochsalz Feb 10 '21 at 18:06
  • sorry trying to answer fast to lift a no more question ban – pippo1980 Feb 10 '21 at 18:14
  • https://stackoverflow.com/questions/45384429/how-to-execute-a-python-script-in-a-different-directory: python ../b.py, python 'new dir'/yourscript.py – pippo1980 Feb 10 '21 at 18:20
0

on windows you need to specify complete path

import os
cwd=os.getcwd()
os.chdir(f'{cwd}/foo/bar/baz.py')    
shajahan
  • 102
  • 3
  • thanks for the answer, but I was specifically looking for a way that doesn't edit the script, as I said in my question. – kochsalz Feb 10 '21 at 18:06