1

Can I run a Python folder or directory as a whole to execute all the .py files in it?
Edit: I'm using Windows Powershell.

3 Answers3

7

For bash, This was already answered at Run all Python files in a directory

You can run:

for f in *.py; do python "$f"; done

If you're on Powershell, You can use:

Get-Childitem -Path c:\path\to\scripts -Filter *.py | % {python $_.FullName}

EDIT: Like Duncan said, This is a shorter solution on Powershell:

ls C:\path\to\scripts\*.py | %{ python $_.Fullname}
Community
  • 1
  • 1
Alok Mysore
  • 606
  • 3
  • 16
1

Try this:

import os
path = 'path\\to\\your\\directory\\'
files = os.listdir (path)
for i in files:
    if i.endswith('.py'):
        os.system("python "+path+i)
Andersson
  • 51,635
  • 17
  • 77
  • 129
0

In Powershell you can use:

Get-Childitem -Path c:\to\folder\ -Filter *.py | % {& $_.FullName}
Hexaquark
  • 592
  • 2
  • 7
  • 22