我注意到我有两种替代方法可以在 Python 脚本中写入 Linux 中的文件。我可以创建一个 Popen 对象并使用 shell 重定向(例如“>”或“>>”)写入文件 - 或者我可以使用文件对象(例如 open()、write()、close())。
我已经玩了一段时间,并注意到如果我需要使用其他 shell 工具,使用 Popen 涉及的代码更少。例如,下面我尝试获取文件的校验和并将其写入以 PID 作为唯一标识符命名的临时文件。(我知道如果我再次调用 Popen,$$ 会改变,但假装我不需要):
Popen("md5sum " + filename + " >> /dir/test/$$.tempfile", shell=True, stdout=PIPE).communicate()[0]
下面是使用文件对象的(匆忙编写的)粗略等价物。我使用 os.getpid 而不是 $$ 但我仍然使用 md5sum 并且仍然必须调用 Popen。
PID = str(os.getpid())
manifest = open('/dir/test/' + PID + '.tempfile','w')
hash = Popen("md5sum " + filename, shell=True, stdout=PIPE).communicate()[0]
manifest.write(hash)
manifest.close()
这两种方法有什么优点/缺点吗?我实际上正在尝试将 bash 代码移植到 Python 并希望使用更多 Python,但我不确定我应该采用哪种方式。