You are not logged in. Please login at www.codechef.com to post your questions!

×

[closed] 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.

asked 20 Feb, 22:57

bansal1232's gravatar image

4★bansal1232
2.6k110
accept rate: 16%

closed 27 Feb, 17:13

The question has been closed for the following reason "The question is answered, right answer was accepted" by bansal1232 27 Feb, 17:13


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

link

answered 23 Feb, 20:25

inovation123's gravatar image

4★inovation123
4136
accept rate: 11%

edited 23 Feb, 20:26

Thanks Buddy! Finally got the right answer.

(23 Feb, 21:24) bansal12324★

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 :)

(23 Feb, 21:27) inovation1234★

pipe? Can you explain it?

(23 Feb, 21:31) bansal12324★
1

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.

(23 Feb, 21:46) inovation1234★

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

link

answered 20 Feb, 23:25

exarchias123's gravatar image

0★exarchias123
213
accept rate: 0%

edited 20 Feb, 23:37

Ok @exarchias123, i will wait for the snippets.

(20 Feb, 23:30) bansal12324★

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.

(20 Feb, 23:39) exarchias1230★

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

(20 Feb, 23:39) bansal12324★
1

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

(20 Feb, 23:42) exarchias1230★

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

(20 Feb, 23:49) bansal12324★

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.

link

answered 27 Feb, 03:04

exarchias123's gravatar image

0★exarchias123
213
accept rate: 0%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×717
×386
×141
×109

question asked: 20 Feb, 22:57

question was seen: 895 times

last updated: 27 Feb, 17:13