在Python中整体运行文件夹/目录

在Python中整体运行文件夹/目录,python,powershell,directory,Python,Powershell,Directory,我可以整体运行Python文件夹或目录来执行其中的所有.py文件吗? 编辑:我正在使用Windows Powershell。对于bash,此问题已在中得到解答 您可以运行: for f in *.py; do python "$f"; done 如果您使用的是Powershell,则可以使用: Get-Childitem -Path c:\path\to\scripts -Filter *.py | % {python $_.FullName} Get-Childitem -Path c:\

我可以整体运行Python文件夹或目录来执行其中的所有.py文件吗? 编辑:我正在使用Windows Powershell。

对于bash,此问题已在中得到解答

您可以运行:

for f in *.py; do python "$f"; done
如果您使用的是Powershell,则可以使用:

Get-Childitem -Path c:\path\to\scripts -Filter *.py | % {python $_.FullName}
Get-Childitem -Path c:\to\folder\ -Filter *.py | % {& $_.FullName}
编辑:正如邓肯所说,这是Powershell上较短的解决方案:

对于bash,这已经在

您可以运行:

for f in *.py; do python "$f"; done
如果您使用的是Powershell,则可以使用:

Get-Childitem -Path c:\path\to\scripts -Filter *.py | % {python $_.FullName}
Get-Childitem -Path c:\to\folder\ -Filter *.py | % {& $_.FullName}
编辑:正如邓肯所说,这是Powershell上较短的解决方案:

试试这个:

import os
path = 'path\\to\\your\\directory\\'
files = os.listdir (path)
for i in files:
    if i.endswith('.py'):
        os.system("python "+path+i)
试试这个:

import os
path = 'path\\to\\your\\directory\\'
files = os.listdir (path)
for i in files:
    if i.endswith('.py'):
        os.system("python "+path+i)

在Powershell中,您可以使用:

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

在Powershell中,您可以使用:

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

您的意思是运行特定文件夹中的所有.py脚本?一个接一个?如果您可以使用bash,您可以在读取行时尝试类似ls*.py |的东西;做python$line;是否在单独的进程中或在同一进程中全部运行?是否在特定文件夹中运行所有.py脚本?一个接一个?如果您可以使用bash,您可以在读取行时尝试类似ls*.py |的东西;做python$line;是否在单独的进程中或在同一进程中?我确实看到了这个问题,但我使用的是Windows Powershell。@始终\u困惑,请在问题中提及您在Powershell/cmd上,或将其添加为标记。如果编写脚本,我会完整地编写Get ChildItem,但是如果这是以交互方式进行的,那么只需执行ls*.py |%{python$uwu.FullName}就要短得多。我确实看到了这个问题,但是我使用的是Windows Powershell。@总是很困惑,请在问题中提到您在Powershell/cmd上,或者将其作为标记添加。如果编写脚本,我会完整地写出Get ChildItem,但是,如果以交互方式执行此操作,则只需执行ls*.py |%{python$uz.FullName}就可以缩短时间