python - 将列表变量传递给 Subprocess.call 函数

标签 python

我试图在调用 subprocess.callfor 循环内传递变量 line 来计算 unix 命令cksum.

with open('./my_filelist', 'r') as f:
    for line in f.readlines():
        print "Printing Checksum --->"
        print ""
        subprocess.call(["cksum",line])

应打印 ./my_filelist 中所有文件的 cksum

最佳答案

file.readlines 返回的行方法全部以换行符结尾(如果文件不以尾随换行符结尾,则最后一行除外)。您应该去掉尾随换行符的行;否则,当传递到 cksum 时,换行符将被视为文件名的一部分。作为参数:

subprocess.call(["cksum", line.rstrip()])

关于python - 将列表变量传递给 Subprocess.call 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55404580/

相关文章:

python sklearn : Dynamically set parameters for TfIdfVectorizer

python - Spark 中的协同过滤

Python3 正则表达式 : Remove all characters except/and | from string

python - Unicode 应用程序引擎 Python

python - 使用文件将环境变量加载到 Google Cloud Build

python - 将唯一对象列表与自定义函数进行比较

python - staticmethod 和 classmethod 装饰器可以用纯 Python 实现吗?

python - ValueError : Input 0 is incompatible with layer simple_rnn_1: expected ndim=3, 在 Keras 中发现 ndim=2

python - 将 Pandas 数据框切片到包含值的所有列的第一个实例

python - 计算与 Pandas 的滚动相关性