Python执行其他进程的临时目录?
Python temporary directory to execute other processes?
我在 Python 中有一串 Java 源代码,我想编译、执行和收集输出(stdout 和 stderr)。不幸的是,据我所知,javac
和 java
需要真实文件,所以我必须创建一个临时目录。
最好的方法是什么? tempfile 模块似乎旨在创建仅对 Python 进程可见的文件和目录。但在这种情况下,我也需要 Java 才能看到它们。但是,我还希望尽可能智能地处理其他内容(例如完成后删除文件夹或使用适当的系统临时文件夹)
tempfile.mkstemp
创建一个通常在文件系统中可见的文件,returns 您也可以看到路径。您应该能够使用它来创建您的输入和输出文件 - 假设 javac
将自动覆盖输出文件(如果它存在)如果您系统上的其他进程没有行为不当,则应该没有竞争条件。
tempfile.NamedTemporaryFile
and tempfile.TemporaryDirectory
非常适合您的目的。生成的对象有一个 .name
属性,它提供了一个 java
/javac
可以很好处理的文件系统可见名称,只需确保:
- 如果编译器坚持使用
.java
扩展名命名文件,请适当设置 suffix
- 在将
NamedTemporaryFile
的 .name
交给外部进程之前,始终在文件句柄上调用 .flush()
,否则它可能(通常会)看到不完整的文件
如果您不希望 Python 在关闭对象时清理文件,请将 delete=False
传递给 NamedTemporaryFile
的构造函数,或者使用 mkstemp
和 mkdtemp
函数(创建对象,但不会为您清理它们)。
例如,您可以这样做:
# Create temporary directory for source and class files
with tempfile.TemporaryDirectory() as d:
# Write source code
srcpath = os.path.join(d.name, "myclass.java")
with open(srcpath, "w") as srcfile:
srcfile.write('source code goes here')
# Compile source code
subprocess.check_call(['javac', srcpath])
# Run source code
# Been a while since I've java-ed; you don't include .java or .class
# when running, right?
invokename = os.path.splitext(srcpath)[0]
subprocess.check_call(['java', invokename])
... with block for TemporaryDirectory done, temp directory cleaned up ...
我在 Python 中有一串 Java 源代码,我想编译、执行和收集输出(stdout 和 stderr)。不幸的是,据我所知,javac
和 java
需要真实文件,所以我必须创建一个临时目录。
最好的方法是什么? tempfile 模块似乎旨在创建仅对 Python 进程可见的文件和目录。但在这种情况下,我也需要 Java 才能看到它们。但是,我还希望尽可能智能地处理其他内容(例如完成后删除文件夹或使用适当的系统临时文件夹)
tempfile.mkstemp
创建一个通常在文件系统中可见的文件,returns 您也可以看到路径。您应该能够使用它来创建您的输入和输出文件 - 假设 javac
将自动覆盖输出文件(如果它存在)如果您系统上的其他进程没有行为不当,则应该没有竞争条件。
tempfile.NamedTemporaryFile
and tempfile.TemporaryDirectory
非常适合您的目的。生成的对象有一个 .name
属性,它提供了一个 java
/javac
可以很好处理的文件系统可见名称,只需确保:
- 如果编译器坚持使用
.java
扩展名命名文件,请适当设置suffix
- 在将
NamedTemporaryFile
的.name
交给外部进程之前,始终在文件句柄上调用.flush()
,否则它可能(通常会)看到不完整的文件
如果您不希望 Python 在关闭对象时清理文件,请将 delete=False
传递给 NamedTemporaryFile
的构造函数,或者使用 mkstemp
和 mkdtemp
函数(创建对象,但不会为您清理它们)。
例如,您可以这样做:
# Create temporary directory for source and class files
with tempfile.TemporaryDirectory() as d:
# Write source code
srcpath = os.path.join(d.name, "myclass.java")
with open(srcpath, "w") as srcfile:
srcfile.write('source code goes here')
# Compile source code
subprocess.check_call(['javac', srcpath])
# Run source code
# Been a while since I've java-ed; you don't include .java or .class
# when running, right?
invokename = os.path.splitext(srcpath)[0]
subprocess.check_call(['java', invokename])
... with block for TemporaryDirectory done, temp directory cleaned up ...