繁体   English   中英

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

[英]Running a folder/directory as a whole in Python

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

对于bash ,已经在运行目录中的所有Python文件中得到了解答

您可以运行:

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

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

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

EDIT :就像邓肯说的那样,这是Powershell上的一个较短的解决方案:

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

尝试这个:

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:\to\folder\ -Filter *.py | % {& $_.FullName}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM