gpt4 book ai didi

python - 如何从Python中的不同文件夹导入所有文件

转载 作者:行者123 更新时间:2023-12-01 08:50:01 25 4
gpt4 key购买 nike

__init__.py在目录中,我可以通过

from subdirectory.file import *

但我希望导入该子目录中的每个文件;所以我尝试了
from subdirectory.* import *

这没有用。有什么建议么?

最佳答案

在尝试了一些不同的解决方案后找到了这个方法(如果你在相邻的目录中有一个名为“文件夹”的文件夹):

for entry in os.scandir('folder'):
if entry.is_file():
string = f'from folder import {entry.name}'[:-3]
exec (string)

关于python - 如何从Python中的不同文件夹导入所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44825974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com