last line chopped from input file

phil hunt zen19725 at zen.co.uk
Sun Aug 21 08:27:50 EDT 2005


On 20 Aug 2005 22:53:42 -0700, Eric Lavigne <lavigne.eric at gmail.com> 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):
>
>  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()
>
>On a side note, I am very new to Python so I would appreciate any
>comments on style, or suggestions for simpler ways to write something
>like this (seems overkill for matching one line of shell), or more
>portable ways to write it (requires '\\' on windows but '/' on linux).

A shorter python program would be:

   os.command("debug\\curve-fit <input.txt >output.txt")

If you don't like the doubled \\, you could write:

   os.command(r"debug\curve-fit <input.txt >output.txt")

For portability regarding \\ versus /, look at the os.path module.

-- 
Email: zen19725 at zen dot co dot uk





More information about the Python-list mailing list