0

我注意到我有两种替代方法可以在 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,但我不确定我应该采用哪种方式。

4

2 回答 2

2

编辑:不推荐使用 md5 模块(但仍然存在),您应该使用hashlib 模块

哈希库版本

归档:

import hashlib
with open('py_md5', mode='w') as out:
    with open('test.txt', mode='ro') as input:
        out.write(hashlib.md5(input.read()).hexdigest())

安慰:

import hashlib
with open('test.txt', mode='ro') as input:
    print hashlib.md5(input.read()).hexdigest()

md5 版本 Python 的md5 模块提供了相同的工具:

import md5
# open file to write
with open('py_md5', mode='w') as out:
    with open('test.txt', mode='ro') as input:
        out.write(md5.new(input.read()).hexdigest())

如果您只想获取 md5 十六进制摘要字符串,您可以将其打印出来,然后将其写入文件:

import md5
# open file to write
with open('test.txt', mode='ro') as input:
    print md5.new(input.read()).hexdigest()
于 2013-03-18T19:41:33.100 回答
2

一般来说,我会写这样的东西:

manifest = open('/dir/test/' + PID + '.tempfile','w')
p = Popen(['md5sum',filename],stdout=manifest)
p.wait()
manifest.close()

这避免了任何 shell 注入漏洞。您还知道 PID,因为您没有获取生成的子 shell 的 PID。

于 2013-03-18T19:30:34.653 回答