简体   繁体   English

运行可执行文件,该可执行文件从文件读取输入并打印到另一个文件

[英]Running an executable that reads input from a file and prints to another file

I am trying to run a file, map.py, from within a Python program. 我正在尝试从Python程序中运行map.py文件。 My code is as follows: 我的代码如下:

import sh

exe = sh.Command("./path/to/map.py")
file_in = open("./path/to/inputfile") #inputfile has no extension
out_file = "./path/to/outputfile" #outputfile has no extension
exe(_in=file_in, _out=out_file)

After running this code, I get the following error: 运行此代码后,出现以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/vagrant/venv/lib/python3.5/site-packages/sh.py", line 1427, in __call__
    return RunningCommand(cmd, call_args, stdin, stdout, stderr)
  File "/vagrant/venv/lib/python3.5/site-packages/sh.py", line 774, in __init__
    self.wait()
  File "/vagrant/venv/lib/python3.5/site-packages/sh.py", line 792, in wait
    self.handle_command_exit_code(exit_code)
  File "/vagrant/venv/lib/python3.5/site-packages/sh.py", line 815, in handle_command_exit_code
    raise exc
sh.ErrorReturnCode_255:

  RAN: /vagrant/exec/word_count/map.py

  STDOUT:


  STDERR:

Here is a dummy example of what map.py would be doing: 这是map.py将要执行的虚拟示例:

print("hi")

There should be some stdout stuff though. 不过应该有一些标准输出的东西。 The program just doesn't seem to be running for me and I'm not sure why. 该程序似乎并没有为我运行,我不确定为什么。 I've been staring at this for hours, I would really appreciate the help! 我已经盯着看了好几个小时,非常感谢您的帮助!

If you want to run that code in your current python shell, you should make the parent folder modules and import that file and access it directly. 如果要在当前的python shell中运行该代码,则应制作父文件夹模块并导入该文件并直接访问它。

If you want to run the file in a separate process and get the results use subprocess.check_output 如果要在单独的进程中运行文件并获取结果,请使用subprocess.check_output

If you want to read from one file and write it to another. 如果要从一个文件读取并将其写入另一个文件。 You could do something like this 你可以做这样的事情

input_f = open(input, 'r')
output_f = open('workfile', 'w')
lines = input_f.readlines()
for i in lines:
    #replicates whatever map.py does
    output_f.write(new_line)
    output_f.flush()

I think that will work for what you are trying to do. 我认为这将适合您的尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在文件的两次打印之间输入 - Input in between prints from a file 需要帮助来创建一个Python脚本,该脚本从txt文件中读取主机名,对主机名执行ping操作,解析IP并将其打印到另一个文本文件 - Need help creating a Python script that reads hostnames from a txt file, pings the hostname, resolves the IP and prints it to another text file 从Python文件输入可执行文件 - Input on executable file from Python file 如何创建一个从文本文件中读取数据并将其打印在两个不同部分的程序 - how to create a program that reads the data from the text file and prints it out in two different sections Pyinstaller 可执行文件未运行 - Pyinstaller Executable File Not Running 来自另一个文件的输入 - Input from another file 读取投票文本文件并打印结果而不打印正确数字的程序 - Program that reads a vote text file and prints the outcome not printing the right numbers 如何从 jupyter 笔记本制作一个 python 可执行文件,从配置文件读取要在主程序中读取的文件路径? - How to make a python executable file from jupyter notebook that reads path of files to be read in the main program from a configuration file? Pyinstaller 可执行文件未正常运行 - Pyinstaller Executable file not running properly Python从文件连续读取 - Python Consecutive Reads From File
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM