0
import sys
import subprocess
def generateFiles(numFiles):
    for i in range(0,numFiles):
        postfixDir = str(i)
        subprocess.check_call(['mkdir','example' + postfixDir])
    return

def main():
    numFiles = int(sys.argv[1])
    generateFiles(numFiles)

main()

after i enter commandline python .\batchingCommands.py 5

it showing the following erros:

PS E:\Projects\Python_Projects\Python_Automation\Scripts> python .\batchingCommands.py 5
Traceback (most recent call last):
  File "E:\Projects\Python_Projects\Python_Automation\Scripts\batchingCommands.py", line 13, in <module>
    main()
  File "E:\Projects\Python_Projects\Python_Automation\Scripts\batchingCommands.py", line 11, in main
    generateFiles(numFiles)
  File "E:\Projects\Python_Projects\Python_Automation\Scripts\batchingCommands.py", line 6, in generateFiles
    subprocess.check_call(['mkdir','example' + postfixDir])
  File "E:\Development_tools\Python39\lib\subprocess.py", line 368, in check_call
    retcode = call(*popenargs, **kwargs)
  File "E:\Development_tools\Python39\lib\subprocess.py", line 349, in call
    with Popen(*popenargs, **kwargs) as p:
  File "E:\Development_tools\Python39\lib\subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "E:\Development_tools\Python39\lib\subprocess.py", line 1420, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

i'm trying to create directory automatically

maersa123
  • 19
  • 3
  • Does this answer your question? [How to create new folder?](https://stackoverflow.com/questions/1274405/how-to-create-new-folder) – mkrieger1 Nov 06 '22 at 20:52

1 Answers1

2

Python is telling you it asked the operating system to run the program mkdir, which doesn't exist. Unlike in unix, mkdir isn't an actual program on Windows - it's built into powershell and command.com themselves.

To instruct python to run the given command as arguments to a shell - instead of a raw program - use the shell kwarg.

>>> subprocess.check_call(['mkdir', 'foo'], shell=True)
0
tonymke
  • 752
  • 1
  • 5
  • 16
  • As an aside, you would be better off using the os module's mkdir function, which functions the same on every platform. See https://docs.python.org/3/library/os.html#os.mkdir – tonymke Nov 06 '22 at 20:55