last line chopped from input file

E

Eric Lavigne

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

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?

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).
 
P

phil hunt

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.
 
E

Eric Lavigne

A shorter python program would be:
os.command("debug\\curve-fit <input.txt >output.txt")

I tried this program:
import os
os.command("debug\\curve-fit <input.txt >output.txt")

My error message is:
AttributeError: 'module' object has no attribute 'command'

I also could not find os.command in the help files. My Python version
is 2.4 (latest is 2.4.1, just a bug-fix release).
 
A

Alan Kemp

I tried this program:
import os
os.command("debug\\curve-fit <input.txt >output.txt")

My error message is:
AttributeError: 'module' object has no attribute 'command'

I also could not find os.command in the help files. My Python version
is 2.4 (latest is 2.4.1, just a bug-fix release).

I imagine thats was a typo for:

Alan
 
G

gene tani

or: (for long-running Win32 processes)

os.startfile(r'/relative/path/to/app')

http://docs.python.org/lib/os-process.html

under linux/BSD/solaris, i've run into situations where PATH and other
environmental var s aren't what you expect (they're from the
/etc/profile system defaults, not from your .bashrc). I can't remember
if anything like that happens in windows.
 
J

Jeff Schwab

Eric said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top