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

1 python directory powershell

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

Alo*_*ore 7

对于bash,在目录中运行所有Python文件已经回答了这个问题

你可以运行:

for f in *.py; do python "$f"; done
Run Code Online (Sandbox Code Playgroud)

如果你在Powershell,你可以使用:

Get-Childitem -Path c:\path\to\scripts -Filter *.py | % {python $_.FullName}
Run Code Online (Sandbox Code Playgroud)

EDIT:像Duncan所说,这是一个较短的解决方案Powershell:

ls C:\path\to\scripts\*.py | %{ python $_.Fullname}
Run Code Online (Sandbox Code Playgroud)

  • 如果编写一个脚本,我会完全写出"Get-ChildItem",但是如果这是以交互方式完成的,那么只需要执行`ls*.py | %{python $ _.FullName}` (4认同)