I am new to using terminal in Mac. When I type any python3 command it only checks the users folder on my PC, HOW can I change the directory to open a folder in the users section and check for the .py file there?
Asked
Active
Viewed 50 times
0
-
cd to that directory first, before running python. – John Gordon Jan 18 '21 at 01:46
-
Does this answer your question? [Find "home directory" in Python?](https://stackoverflow.com/questions/10170407/find-home-directory-in-python) – l'L'l Jan 18 '21 at 01:54
4 Answers
1
Access the desired path using cd
command
cd path/to/access
Then you can run the python command to run the scripts.

frab
- 1,162
- 1
- 4
- 14
0
If you know the name of the folder in which you want to check, you can change the current python directory using: os.chdir
https://docs.python.org/3/library/os.html#os.chdir
In that case it doesn't matter from where you're running your python script.

robbo
- 525
- 3
- 11
0
You can use the OS library which comes shipped with Python. to change the current directory inside the script, you can simply write:
import os
os.chdir("newDir/")
if you should want to get all directories which are in the user's current location, you can use the method:
os.listdir()
Hope that helps you out.

Bialomazur
- 1,122
- 2
- 7
- 18