last line chopped from input file

Jeff Schwab jeffrey.schwab at rcn.com
Sun Aug 21 16:09:49 EDT 2005


Eric Lavigne wrote:
> Here is a shell command (MS-DOS):
>   debug\curve-fit <input.txt >output.txt
> 
> And here is a Python script that *should* do the same thing (and almost
> does):

Python equivalent is roughly:

	import os
	import subprocess

	subprocess.Popen([os.path.join("debug", "curve-fit")],
         	stdin=file("input.txt"), stdout=file("output.txt", 'w')).wait()


>   import os
> 
>   inputfilename = 'input.txt'
>   outputfilename = 'output.txt'
> 
>   inputfile = open(inputfilename,'r')
>   outputfile = open(outputfilename,'w')
>   inputstream,outputstream = os.popen2("debug\\curve-fit")
>   inputstream.write(inputfile.read())
>   inputfile.close()
>   inputstream.close()
>   outputfile.write(outputstream.read())
>   outputstream.close()
>   outputfile.close()
> 
> In the shell command, my curve-fit program processes the entire input
> file. In the Python script, my curve-fit program processes all except
> for the last line. Adding an extra newline to the end of my input file
> fixes this problem. What did I do wrong that led to this small
> difference?

No idea.  Your version works fine for me.



More information about the Python-list mailing list