Run code in a different location

id like to try and run a program ive written in python, in a different location to where the file is currently saved, for example:


I have the program (currently named dl.py) saved in Folder A


I can open terminal in Folder B and want to have dl.py run in folder B, and perform all of its actions within that folder, while it is still saved in Folder A.


as it is a downloading agent I have written and am testing some scenarios, and don't want to clog Folder B with the file itself, as it also uses some config files


any advice on this would be much appreciated


(im amateur with Mac terminal at best)

MacBook Air 13″, macOS 12.1

Posted on Jan 12, 2023 12:01 PM

Reply
Question marked as Top-ranking reply

Posted on Jan 12, 2023 3:48 PM

Specify the path to the script.


The following assumes your nick here is your login shortname on your Mac:


/Users/ryanlarsjohnson/Folder\ A/dl.py


The above also shows the space in the folder name escaped. This is necessary at the command line, as spaces are part of the command line syntax. The other option there is to " double-quote the path. Double quotes around a path are common in shell scripts (more later), as the path contents can include spaces and other characters.


Adjust that path as needed for the local system; for whatever path you have on your particular Mac.


One way to adjust the path? You could use the tilde to represent your home directory. The following is relative to your home directory:


~/Folder\ A/dl.py


The above avoids needing to know what your shortname home folder name is. (As does the HOME environment variable.)


Here's a shell scripting primer published by Apple. It's a little old and is mostly focused on the bash shell, and not the current zsh shell, but is still good introduction to shell scripting.


Introduction


If the script you invoke starts with the Python shebang (as differentiated from the bash or zsh shebang), it'll launch whatever version of Python 3 you've installed, and the rest of the script contains, well, Python code.


#!/usr/bin/env python3
1 reply
Question marked as Top-ranking reply

Jan 12, 2023 3:48 PM in response to ryanlarsjohnson

Specify the path to the script.


The following assumes your nick here is your login shortname on your Mac:


/Users/ryanlarsjohnson/Folder\ A/dl.py


The above also shows the space in the folder name escaped. This is necessary at the command line, as spaces are part of the command line syntax. The other option there is to " double-quote the path. Double quotes around a path are common in shell scripts (more later), as the path contents can include spaces and other characters.


Adjust that path as needed for the local system; for whatever path you have on your particular Mac.


One way to adjust the path? You could use the tilde to represent your home directory. The following is relative to your home directory:


~/Folder\ A/dl.py


The above avoids needing to know what your shortname home folder name is. (As does the HOME environment variable.)


Here's a shell scripting primer published by Apple. It's a little old and is mostly focused on the bash shell, and not the current zsh shell, but is still good introduction to shell scripting.


Introduction


If the script you invoke starts with the Python shebang (as differentiated from the bash or zsh shebang), it'll launch whatever version of Python 3 you've installed, and the rest of the script contains, well, Python code.


#!/usr/bin/env python3

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Run code in a different location

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.