Skip to main content
 首页 » 编程设计

python之在 Python 中作为一个整体运行文件夹/目录

2025年05月04日11lhb25

是否可以将一个Python文件夹或目录作为一个整体来执行其中所有的.py文件?
编辑:我正在使用 Windows Powershell。

请您参考如下方法:

对于 bash,这已经在 Run all Python files in a directory 得到了回答

你可以运行:

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

如果您使用的是 Powershell,您可以使用:

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

编辑:正如 Duncan 所说,这是 Powershell 上的一个较短的解决方案:

ls C:\path\to\scripts\*.py | %{ python $_.Fullname}