How to take input from external file in python

Hello guys! I wanna know about how can i take the input of test cases saved in the external file (‘input.txt’) and also output it to ‘output.txt’ file in python?

Please provide the full code with explanation.

1 Like

That is a great question, (that is the reason that I failed, i didn’t had prefabricated code for handling the test cases).
The solution and the explanation is posted (HERE) but it is written in C++ and is not exactly and not in a form that favors reusability.
give me few days and I will create few snippets about it.
Also here is how i am handling I/O issues:
Input:

fileForInput = open('input.in','r')
...
for N in fileForInput:

output:

fileForOutput = open('output.txt','w')
...
fileForOutput.write(answer)

But you can use the whole of my template if you think that it suits you, (HERE)

Best Regards.
Robert

1 Like

Ok @exarchias123, i will wait for the snippets.

Hello Again.
I inserted few snippets on how i am handling I/O issues with external files, and the template that I am using when I have to do with external files.
I hope it helps!
Best Regards.
Robert.

Can you make the code more concise and short ? please…

sorry. I defend my style for reasons of encapsulation and resusability.
I Apologize for any inconvenience.
Best Regards.
Robert.

1 Like

Sorry to say! I could not able to interpret your code.

You just write simple program and then run it from command line in any platform like Windows/ Linux.

python program.py < input.txt > output.txt

<,> are redirection operators which simply redirects the stdin and stdout to input.txt and output.txt. This is the easiest way.

Alternatively, you can do the following

import sys
sys.stdin=open(‘input.txt’,‘r’)
sys.stdout=open(‘output.txt’,‘w’)

Alternatively you can do the following
input=open(‘input.txt’,‘r’)
ouput=open(‘ouput.txt’,‘w’)
n=input.read()
output.write(n)

I prefer method 1 as it is simple and no need of file handling and this helps a lot in Codejam, FaceBook HackerCup. Hope it helps

3 Likes

Thanks Buddy! Finally got the right answer.

Input redirection is the easiest method to get ur job done and is quite handy in contests like CodeJam etc. Alternatively u can create a pipe to do the same task. So this gives 4th approach :slight_smile:

pipe? Can you explain it?

pipe is FIFO and is commonly used in Unix OS. It is used to transfer information from one sub process to another. So you can create a pipe for reading input from input file, another pipe to write output in output file and then share data among the two pipes.

1 Like

I was promise to write a prefabricated function that handles cases.
Here it is, I hope it helps:

#this the function that breaks the stream to cases
#this function is inspired by the DWNLD problem on codechef
#please use global i and countForDwnldN outside the function
#also use the repair function to repair the caserArray

def caser(inputFromN, inputForI):
    global countForDwnldN, dwnldK, caserArray, i
    i = inputForI
    print("caser started")
    print("i now is " + str(i))
    caserTemporalValues = ""
    caserTemporalValues = inputFromN.split()
    if int(countForDwnldN) == 0:
        print("countForDwnldN is 0")
        print("caserTemporalValues is " + str(caserTemporalValues))
        dwnldK = caserTemporalValues[0]
        print("dwnldK is " + str(dwnldK))
        countForDwnldN = caserTemporalValues[1]
        print("countForDwnldN is " + str(countForDwnldN))
        i = int(i) + 1
        caserArray.append([])
        caserArray[i - 1].append(dwnldK)
    else:
        print("countForDwnldN is NOT 0")
        print("countForDwnldN is " + str(countForDwnldN))
        print("i is " + str(i))
        caserArray[i - 1].append(caserTemporalValues)
        countForDwnldN = int(countForDwnldN) - 1
        print("caserArray is " + str(caserArray[i - 1]))
        # i = int(i) + 1



# it needs to run after the caser()
# it repairs if necessary, (and if possible), the caserArray
# it repairs caserArray by removing the empty trailer.

def caseReapairer(howManyCasesParameter):
    if len(caserArray) > int(howManyCasesParameter):
        caserArray.pop()

I have to apologize if it seems bit chaotic. It is designed to be prefabricated.
Best Regards.
Robert.

//