The terminal, folders, and your first project
Move around your folders from the terminal, create a project folder, run files, and learn when Python's interactive shell is the right tool.
- 1Encounter
- 2Understand
- 3Worked
- 4Predict
- 5Apply
- 6Stretch
The problem we are solving
In the last mission you installed Python and ran a file. But something happened quietly in the middle of it — before running the file, you had to go to its folder in the terminal.
That "going" is what traps beginners more than anything else. The code is fine, Python is fine, but the terminal is sitting in a different folder — and Python reports that the file does not exist.
At work you will do this a few hundred times a day. So today we turn the terminal from an unfamiliar black screen into a tool you know.
By the end of this mission you can
- Tell which folder the terminal is currently in
- Move from one folder to another, and step back up one level
- Create a project folder and run a file inside it
- Open and close Python's interactive shell (the REPL)
- Explain how the REPL differs from a
.pyfile — this one confusion wastes a lot of time
Prerequisites: Installing Python and running your first program.
What the terminal actually is
The terminal is a place where you instruct the computer by typing instead of clicking. And every terminal window is always "standing" inside some folder. That folder is called the current directory.
This is the whole idea. When you type python hello.py, what you are really saying is: "run the `hello.py` file in the folder I am standing in right now." If the file is in some other folder, Python will not find it — because it does not look anywhere else.
Open PowerShell and you will usually see something like:
PS C:\Users\Rafi>The C:\Users\Rafi part is telling you where you are.
Four commands that cover the job
pwd — where am I?
pwdPath
----
C:\Users\Rafidir — what is in here?
dirIt lists the folders and files. Running dir before you run a file is a good habit — it confirms the file you are about to run is actually there.
cd — go to another folder
cd D:\python-labTwo dots step one level back up:
cd ..To move to the D: drive in PowerShell, cd D:\ is enough.
Type less. Write the first two or three letters of a folder or file name and press Tab — the terminal fills in the rest. Hand-typing names is the biggest single source of typos, and Tab removes it.
mkdir — make a new folder
mkdir lesson-02Set up a project folder
If you scatter your files across the Desktop while learning, you will not find anything a week later. So pick one place and give each mission its own folder:
cd D:\
mkdir python-lab
cd python-lab
mkdir lesson-02
cd lesson-02
pwdThat last command should answer:
Path
----
D:\python-lab\lesson-02Now create a file called notes.py in this folder containing:
print("Folder:", "lesson-02")
print("Squares:", 2 * 2, 3 * 3, 4 * 4)And run it:
python notes.pyFolder: lesson-02
Squares: 4 9 16Python's interactive shell
Type just python in the terminal and press Enter, and Python itself opens up:
pythonPython 3.12.0 (tags/v3.12.0) on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>That >>> means Python is waiting for a line from you. Whatever you write here is answered immediately:
>>> 2 + 3
5
>>> "Python"
'Python'
>>> len("terminal")
8This is called the REPL — read, evaluate, print, loop. It is excellent for checking something small: whether a calculation is right, or what a function hands back.
To leave:
>>> exit()The difference that costs people hours: in the REPL, typing2 + 3shows you5on its own. But put only2 + 3in a.pyfile and run it, and nothing appears — Python does the arithmetic, throws the result away, and stays silent. In a file you have to write
When it breaks
python : The term 'python' is not recognized... Python is not on PATH. See the "When it breaks" section of the previous mission — re-run the installer and tick the box under Modify.
can't open file '...notes.py': [Errno 2] No such file or directory The terminal is in the wrong folder, or the filename has a typo. Run pwd to see where you are, then dir to see whether the file is there.
cd : Cannot find path 'D:\python-lab' because it does not exist The folder has not been created yet, or the name is wrong. Create it with mkdir, and press Tab next time you type the name.
The terminal seems stuck, showing only >>> You are inside the REPL, not outside it. dir and python notes.py will not work here — those are terminal commands, not Python. Type exit() to leave.
The folder name has spaces in it Quote the whole path: cd "D:\my python lab". Naming folders without spaces avoids the problem entirely, which is why python-lab and my_notes are better habits.
Step 4 of 6 — Predict
Check your understanding
This two-line file is run with python notes.py. What appears in the terminal?
2 + 3
print("done")- Adone
- B5 done
- C5
- DNothing — the file raises an error
The file notes.py is inside the lesson-02 folder, but the terminal is standing one level up, in python-lab. What happens when you run python notes.py?
- APython errors that it cannot open the file — it only looks in the current folder
- BPython searches the subfolders too and runs the file
- CThe file runs, but the output is not shown
- DPython asks you where the file is
Your terminal is showing >>> and you want to list the files in the folder. What do you do?
- AType `exit()` to leave the REPL, then run `dir`
- BType `dir` directly — terminal commands work inside the REPL
- CType `print(dir)`
- DClose the terminal window and open a new one; that is the only way
Answering needs an account
Sign in to check your answers
The questions are above, and working them out in your head is the part that matters. Sign in to see the answers, the explanations and the three-level hints.
Your turn
From the terminal, using commands only:
- Create a folder called
practiceinsideD:\python-lab - Move into it and confirm with
pwd - Create a file there called
about.pythat prints your name and today's date - Run it with
python about.py - Now
cd ..to step back one level and runpython about.pyagain — what happened?
That last step is the real lesson. Once you have seen the failure on purpose, you will recognise its cause in one second when it shows up in real work.
There is more in this mission
What you read above is the complete mission — nothing was cut out of it. Pro adds:
- What actually happens underneath — how Python runs this
- The full solution to the exercise, worked step by step
- Harder extra exercises
- The interview question on this topic, with a model answer
Step 6 of 6
Stretch — the chapter quiz
Ten questions from easy to hard. The last ones are difficult on purpose.
Sign in to take the quiz