[Python] how to take a file as input stream in Python

Discussão em 'Python' iniciado por Stack, Março 19, 2025.

  1. Stack

    Stack Membro Participativo

    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+B<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?

    Continue reading...

Compartilhe esta Página