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.

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. Commented Feb 10, 2021 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... Commented Mar 15, 2022 at 9:58

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the answer, but I was specifically looking for a way that doesn't edit the script, as I said in my question.
sorry trying to answer fast to lift a no more question ban
stackoverflow.com/questions/45384429/…: python ../b.py, python 'new dir'/yourscript.py
0

on windows you need to specify complete path

import os
cwd=os.getcwd()
os.chdir(f'{cwd}/foo/bar/baz.py')    

1 Comment

thanks for the answer, but I was specifically looking for a way that doesn't edit the script, as I said in my question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.