1

I'm trying to learn Python from a tutorial. I'm supposed to create a folder using mkdir, but when I try that, I see this result:

> python
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information. 
>>> mkdir mystuff
  File "<stdin>", line 1
    mkdir mystuff
                ^
SyntaxError: invalid syntax

I couldn't figure this out from the tutorial. What is wrong here, and how do I fix it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
user3538364
  • 27
  • 1
  • 2
  • 1
    This is functionally the same problem as [Why does "pip install" inside Python raise a SyntaxError?](https://stackoverflow.com/questions/8548030), but that question was asked narrowly (and attracted some answers that missed the point or buried the lede). – Karl Knechtel Jan 25 '23 at 18:18

5 Answers5

5

The command mkdir mystuff needs to be used from the system command line, not Python.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
user2357112
  • 260,549
  • 28
  • 431
  • 505
2

To create a new folder from within Python, use:

import os
os.mkdir(path)

To run the shell command from within Python, use:

os.system('mkdir path')
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
PYPL
  • 1,819
  • 1
  • 22
  • 45
1

Before running the mkdir command, press Ctrl+Z to exit Python. It should look like this:

> python
ActivePython 2.6.5.12 (ActiveState Software Inc.) based on
Python 2.6.5 (r265:79063, Mar 20 2010, 14:22:52) [MSC v.1500 32 bit (Intel)] 
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z
> mkdir mystuff
> cd mystuff

The screen will display ^Z to indicate that Ctrl+Z was pressed.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Andrew
  • 11
  • 1
1

Exit from Python first by typing quit() and pressing the Enter key, and then use the mkdir mystuff command. This command should be used outside of python.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
-1

mkdir is used at the command line to make a directory (folder). For example, mkdir mystuff makes a directory named mystuff.

This needs to be run with the command line, not python.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
bezzoon
  • 1,755
  • 4
  • 24
  • 52