stdin

Is there any way to pass 'stdin' as an argument to another process in python?

雨燕双飞 提交于 2019-11-28 06:57:21
I'm trying to create a script which is using multiprocessing module with python. The script (lets call it myscript.py) will get the input from another script with pipe. Assume that I call the scripts like this; $ python writer.py | python myscript.py And here is the codes; // writer.py import time, sys def main(): while True: print "test" sys.stdout.flush() time.sleep(1) main() //myscript.py def get_input(): while True: text = sys.stdin.readline() print "hello " + text time.sleep(3) if __name__ == '__main__': p1 = Process(target=get_input, args=()) p1.start() this is clearly not working, since

How do you read from stdin in python from a pipe which has no ending

此生再无相见时 提交于 2019-11-28 06:24:09
I've problem to read from Standard input or pipe in python when the pipe is from a "open" (do not know right name) file. I have as example pipetest.py: import sys import time k = 0 try: for line in sys.stdin: k = k + 1 print line except KeyboardInterrupt: sys.stdout.flush() pass print k I run a program that have continues output and Ctrl+c after a while $ ping 127.0.0.1 | python pipetest.py ^C0 I get no output. But if I go via an ordinary file it works. $ ping 127.0.0.1 > testfile.txt this is ended by Ctrl+c after a short while $ cat testfile.txt | python pipetest.py PING 127.0.0.1 (127.0.0.1)

Read unknown number of lines from stdin, C

廉价感情. 提交于 2019-11-28 05:54:07
问题 i have a problem with reading stdin of unknown size. In fact its a table in .txt file, which i get to stdin by calling parameter '<'table.txt. My code should look like this: #include <stdio.h> #include <string.h> int main(int argc,char *argv[]) { char words[10][1024]; int i=0; while(feof(stdin)==0) { fgets(words[i],100,stdin); printf("%s", words[i]); i++; } return 0; } but there is the problem i dont know the nuber of lines, which in this case is 10(we know the number of characters in line -

Using sys.stdin.readline() to read multiple lines from cmd in Python

点点圈 提交于 2019-11-28 05:47:31
问题 I'd like to type in my input from command lines after running if __name__ == "__main__": data = list(map(int, sys.stdin.readline().split())) print(data) n, capacity = data[0:2] values = data[2:(2 * n + 2):2] weights = data[3:(2 * n + 2):2] A sample input could be: 2 40 20 2 30 3 My questions are: 1) How to create the list data using my input? 2) How can I let Python know I have finished the input and it should execute the rest of the code? 回答1: The solution to this problem depends on the OS

What is the standard input buffer?

邮差的信 提交于 2019-11-28 04:48:56
问题 #include <stdio.h> int main(void) { int c; c = getchar(); putchar(c); c = getchar(); putchar(c); c = getchar(); putchar(c); return 0; } I want to understand why the function that is called three times working with a line that was entered only once. Some guy explained, that we working with the standard input buffer in this situation, and that is a piece of memory. I want to read something about it. Can you advise me some resources? 回答1: This is a feature of your terminal (the command line

How to display the redirected stdin in Python?

旧街凉风 提交于 2019-11-28 04:03:20
问题 I'm starting on a Python project in which stdin redirection is necessary, using code similar to below: import sys import StringIO s = StringIO.StringIO("Hello") sys.stdin = s a = raw_input("Type something: ") sys.stdin = sys.__stdin__ print("You typed in: "+a) The problem is, after the code runs, the following is displayed: Type something: You typed in: Hello Is there a way to modify my code such that the following is displayed instead? Type something: Hello You typed in: Hello I've been

reading unknown number of integers from stdin (C)

。_饼干妹妹 提交于 2019-11-28 03:48:27
问题 I need to read an input file like : 1 19 20 41 23 2 41 52 43 3 90 91 941 4 512 5 6 51 61 Each odd line is an integer. Each even line is unknown number of integers. It is very easy in C++ while( cin >> k ){ ............ } I'm not so used to C, so I couldnt make it in C. Any ways to do it? 回答1: The way I would do it is to break it down into two operations: read a line, then read the integers in that line. Here is a lazy implementation using the standard C library: char line[1024], *p, *e; long

c++: subprocess output to stdin

☆樱花仙子☆ 提交于 2019-11-28 03:08:07
问题 Suppose I want to call a subprocess from within my program, and I want to read the output from that subprocess into my program. Here is a trivial way to do that: //somefile.cpp system("sub_process arg1 arg2 -o file.out"); //call the subprocess and have it write to file FILE *f = std::fopen("file.out", "r"); //.... and so on We all know that i/o operations are computationally slow. To speed this up, I would like to skip the write-to-file-then-read-from-file step, and instead redirect the

Can i put binary in stdin? C#

蹲街弑〆低调 提交于 2019-11-28 03:07:21
问题 Related to this question encrypt binary with 7z without filenames? In C# how can i put binary in STDin? i was hoping the below would work but it doesnt. And it makes sense. So how do i push a byte[] array? new BinaryWriter(p.StandardInput.FormatProvider); 回答1: Write directly to the base stream: new BinaryWriter(p.StandardInput.BaseStream) 回答2: stdin is just another byte stream, one your program can read from Stream st=Console.OpenStandardInput (); StreamReader sr=new StreamReader(st); etc. In

Receiving data via stdin and storing as a variable/array

你。 提交于 2019-11-28 02:25:07
I am trying to receive data via stdin which I have working: const fs = require('fs'); const readStream = process.stdin; readStream.pause(); readStream.setEncoding('utf8'); readStream.on('data', (data) => { console.log(data); }); readStream.resume(); Now what I need to do is store it as a variable so I can do some calculations before I return it via stdout. Every time I try to so anything with it, like push certain data to an array it repeats the data until the stdin has finished, and I cant access it after it has finished. I cant find any resources online to help me. You can use ReadableStream