1

I want to execute the following command:

python -c "import os, random, string; for _ in range(30): os.mkdir(''.join(random.choice(string.ascii_lowercase) for _ in range(random.randint(3, 10))))"

But I'm getting:

  File "<string>", line 1
    import os, random, string; for _ in range(30): os.mkdir(''.join(random.choice(string.ascii_lowercase) for _ in range(random.randint(3, 10))))
                                 ^
SyntaxError: invalid syntax

Why does this error occur?

Same code with linebreaks and without the semicolon:

import os, random, string

for _ in range(30):
    os.mkdir(''.join(
        random.choice(string.ascii_lowercase) for _ in range(random.randint(3, 10))
        ))
Pastafarianist
  • 833
  • 11
  • 27
  • Python needs white space, so when you one-line it, it doesn't know what your indentation is – Mike Tung Jan 17 '18 at 20:16
  • 1
    @MikeTung not 100% true, `import time; x = time.time(); print(x)` works fine, the real answer is in the dup link from Vaultah – MooingRawr Jan 17 '18 at 20:18

0 Answers0