0

I have this piece of code:

t=int(input())
for i in range(t):
    n,k=map(int, raw_input().split())
    A=map(int, raw_input().split())
    B=map(int, raw_input().split())
    A.sort()
    B.sort(reverse=True)
    flag=True
    while(i<len(A)):
        if A[i]+B[i]<k:
            flag=False
            break
        i+=1
    if flag==True:
        print "YES"
    else:
        print "NO"

And I want to give a text file containing the following data formatted specifically to be given input to such a program. How should I do it? here is the text:

10
8 91
18 73 55 59 37 13 1 33
81 11 77 49 65 26 29 49
18 94
84 2 50 51 19 58 12 90 81 68 54 73 81 31 79 85 39 2
53 102 40 17 33 92 18 79 66 23 84 25 38 43 27 55 8 19
8 88
66 66 32 75 77 34 23 35
61 17 52 20 48 11 50 5
14 45
11 16 43 5 25 22 19 32 10 26 2 42 16 1
33 1 1 20 26 7 17 39 23 34 10 11 38 20
11 59
15 16 44 58 5 10 16 9 4 20 24
37 45 41 46 8 23 59 57 51 44 59
8 32
28 14 24 25 2 20 1 26
4 3 1 11 25 22 2 4
6 57
1 7 42 26 45 14
37 49 42 26 4 11
4 88
25 60 49 4
65 46 85 34
16 9
2 1 1 4 1 7 3 4 3 7 3 1 3 5 6 7
1 1 1 1 4 1 2 1 7 1 1 1 1 1 1 2
1 70
40
38

What should I do in order to give a txt file containing these numbers as input stream to the above mentioned program?

4 Answers 4

3

You could either pass the content as an stdin stream or as an argument from the commmand line then open the file and read it. Assuming that your program is named script.py

# this will read from the stdin
import sys
for line in sys.stdin:
   print(line)

You can use it with cat file | python script.py

if __name__ == '__main__':
     import argparse
     parser = argparse.ArgumentParser()
     parser.add_argument('inputfile')
     args = parser.parse()
     with open(args.inputfile) as handler:
         for line in handler:
             print(line)

This option will be python script.py file

Sign up to request clarification or add additional context in comments.

2 Comments

Isn't there someway in which it can treat the input file as an input given by the user waiting at each newline character for new input being asked by the program?
You mean, without changing your current version of the code that is using input()? Well, input() reads from the stdin, so you can pipe the file contents the same way as the first example cat file | python script.py
3

Building on Dunes answer, all you need is:

with open('file_path.txt','r') as sys.stdin:
        t = int(input())

Comments

1

You can set sys.stdin to be any file-like object you want. The input function will use whatever object is stored there to get input. Here I've used a BytesIO object to use a string for input.

import io
import sys

data = b"""1
8 91
18 73 55 59 37 13 1 33
81 11 77 49 65 26 29 49"""

try:
    with io.BytesIO(data) as sys.stdin:
        # your code here
        i = raw_input() # or input if python 3
        print("i = {!r}".format(i))
        assert i == "1"

finally:
    # restore original standard input
    sys.stdin = sys.__stdin__

Comments

0

While Chintan's answer is straightforward, it requires all your code be in an extra indentation block. The best way to do this without extra indent block is

import sys
sys.stdin = open('input.txt', 'r', encoding='utf-8')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.