last line chopped from input file

Discussion in 'Python' started by Eric Lavigne, Aug 21, 2005.

  1. Eric Lavigne

    Eric Lavigne Guest

    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).
    Eric Lavigne, Aug 21, 2005
    #1
    1. Advertising

  2. Eric Lavigne

    phil hunt Guest

    On 20 Aug 2005 22:53:42 -0700, 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):
    >
    > 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
    phil hunt, Aug 21, 2005
    #2
    1. Advertising

  3. Eric Lavigne

    Eric Lavigne Guest

    >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).
    Eric Lavigne, Aug 21, 2005
    #3
  4. Eric Lavigne

    Alan Kemp Guest

    On Sun, 21 Aug 2005 13:41:14 -0400, Eric Lavigne <>
    wrote:

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


    I imagine thats was a typo for:

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


    Alan
    Alan Kemp, Aug 21, 2005
    #4
  5. Eric Lavigne

    Eric Lavigne Guest


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

    >
    > I imagine thats was a typo for:
    >
    > >>> os.system("debug\\curve-fit <input.txt >output.txt")

    >
    > Alan


    That fixes it. Thanks.
    Eric Lavigne, Aug 21, 2005
    #5
  6. Eric Lavigne

    gene tani Guest

    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.
    gene tani, Aug 21, 2005
    #6
  7. Eric Lavigne

    Jeff Schwab Guest

    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.
    Jeff Schwab, Aug 21, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gabriel Genellina
    Replies:
    4
    Views:
    10,987
    JodyGnumeric
    Dec 10, 2008
  2. scad
    Replies:
    23
    Views:
    1,157
    Alf P. Steinbach
    May 17, 2009
  3. Bill

    String getting chopped off

    Bill, Aug 21, 2003, in forum: ASP General
    Replies:
    1
    Views:
    112
    Curt_C [MVP]
    Aug 21, 2003
  4. Replies:
    10
    Views:
    263
    Robert Klemme
    Oct 11, 2008
  5. Ian M. Asaff

    last-input entered on command line

    Ian M. Asaff, Nov 25, 2010, in forum: Ruby
    Replies:
    0
    Views:
    100
    Ian M. Asaff
    Nov 25, 2010
Loading...

Share This Page