首页 > 解决方案 > 如何在 macOS 中使用 python 附加文件夹中存在的多个 .dmg 文件

问题描述

多个 .dmg 文件存在于特定文件夹中,使用 python 如何迭代地挂载这些 .dmg 文件。我尝试了以下代码

    import os
    path = '/Users/kat/Desktop/pro'
    os.chdir(path)
    for i in os.scandir(path):
     print(i)
     os.system("hdiutil attach i")

但我得到“hdiutil”命令未找到错误。任何建议都会有所帮助。

标签: pythonpython-3.xmacos

解决方案


我想你想要更像这样的东西:

import os, glob

os.chdir('/Users/kat/Desktop/pro')

for i in glob.glob("*.dmg"): 
    print(i) 
    os.system(f'hdiutil attach "{i}"')

Python “f-strings”在此处描述。


推荐阅读