Python:内存中当前工作目录的目录subprocess.call()

2024-06-02 06:25:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个python脚本,它读取METAFONT文件,在上面运行METAPOST(它生成大约250个PostScript文件),在fontforme中导入这些PostScript文件并输出OpenType字体。以下是此脚本的简化版本:

import os,sys,fontforge,glob,subprocess,tempfile,shutil
if __name__ == "__main__":          
    mffile = os.path.abspath(sys.argv[1])
    tempdir = tempfile.mkdtemp()
    font = fontforge.font()
    subprocess.call(
    ['mpost',
    '&mfplain',
    '\mode=localfont;',
    'mag:=100.375;',
    'outputtemplate:="%c.eps";',
    'input %s;' % mffile,
    'bye'],
    stdout=subprocess.PIPE, 
    stderr=subprocess.PIPE,
    cwd=tempdir)
    glyph_files = glob.glob(os.path.join(tempdir, "*.eps"))
    for eps in glyph_files:
        code = int(os.path.splitext(os.path.basename(eps))[0])
        glyph = font.createChar(code)
        glyph.importOutlines(eps, ("toobigwarn", "correctdir"))
    font.generate("font.otf")
    shutil.rmtree(tempdir)
    exit(0)

目前,250个PostScript文件存储在一个临时目录中。为了使事情变得更快,我想把它们存储在内存中。因此,tempdir应该是内存中的一个目录。我试过记忆(类似这样的东西

^{pr2}$

)但我不知道如何访问tempdir中的文件

glyph_files = glob.glob(os.path.join(tempdir, "*.eps"))

有效。在

你对如何访问这些文件有什么建议吗?谢谢您。 (也欢迎使用MemoryFS的替代品。)


Tags: 文件path脚本ossysfilesepstempfile
1条回答
网友
1楼 · 发布于 2024-06-02 06:25:10

根据docsgetsyspath是获得操作系统支持的文件路径的方法。但是,MemoryFS没有由实际文件系统支持,并且不会覆盖fs.base.FS的默认实现,它将引发NoSysPathError。在

内存支持的文件系统实现将取决于您的操作系统。例如,在现代linux中,您可以mount a ^{} partition,它看起来像虚拟文件系统中的其他文件夹,但文件将存储在内存中。{不过,请注意,如果系统正在运行^页,则可能会移出该区域。在

相关问题 更多 >