如何在Python中将一个非常长的字符串从一个脚本传递到另一个脚本?

时间:2017-05-01 15:53:50

标签: python python-2.7

我有两个python脚本sender.pyreciever.py

我正在尝试通过在 os.system() 的命令行发送一个非常长的字符串,从发送方发送到接收方但是它在Ubuntu上给出了奇怪的错误,我无法回忆(两行错误,每行 sh ),而在Windows上则表示命令行太长。

sender.py (最后调用os.system()

class Bob(object):
    def __init__(self):
        print(self.generateReport())
    def generateReport(self):
       # variables used to format report are defined here.
        report = '''some really really long string'''.format(fw_date, release_version, release_date, release_file_tag, crc, checksum[0], checksum[1],
                           checksum[2], checksum[3], checksum[4], checksum[5], pon_firmware, mib_release
                           )
        os.system('python reciever.py '+report)            
        return report


def main():
    try:
        bObj = Bob()
        # miniSVNObj.execute()
    except Exception as ex:
        # print ""
        print(ex)


if __name__ == "__main__":
    main()

reciever.py

import sys
message = "Dear All Concerned, \n"+sys.argv[1]

print(message)

2 个答案:

答案 0 :(得分:2)

命令行的大小受操作系统的限制。应该通过文件读取任意大块数据,标准输入或明确打开的文件。

例如,

merchantId

import subprocess class Bob(object): def __init__(self): print(self.generateReport()) def generateReport(self): # variables used to format report are defined here. report = '''...'''.format(fw_date, release_version, release_date, release_file_tag, crc, checksum[0], checksum[1], checksum[2], checksum[3], checksum[4], checksum[5], pon_firmware, mib_release ) with tempfile.TemporaryFile() as fh: fh.write(report) fh.seek(0) subprocess.call(['python', 'receiver.py', stdin=fh) return report def main(): try: bObj = Bob() # miniSVNObj.execute() except Exception as ex: # print "" print(ex) if __name__ == "__main__": main() 变为

receiver.py

答案 1 :(得分:0)

Receiver可以通过其stdin获取字符串。它是什么: - )