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.
Asked
Active
Viewed 1,696 times
1

always_confused
- 13
- 7
-
You mean to run all `.py` scripts in specific folder? one by one? – Andersson Jul 06 '15 at 07:25
-
If you can use bash, you could try something like `ls *.py | while read line ; do python $line
; done` – Igor Jul 06 '15 at 07:27 -
In separate processes or all in the same process? – cdarke Jul 06 '15 at 07:28
3 Answers
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
-
I did see that question, however I'm using Windows Powershell. – always_confused Jul 06 '15 at 07:31
-
@always_confused, please mention that you're on Powershell/cmd in the question or add it as a tag. – Alok Mysore Jul 06 '15 at 07:32
-
4If writing a script I'd write `Get-ChildItem` out in full, but if this is being done interactively it's much shorter just to do `ls *.py | % { python $_.FullName }` – Duncan Jul 06 '15 at 07:58
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