473,414 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

last line chopped from input file

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

Aug 21 '05 #1
6 1820
On 20 Aug 2005 22:53:42 -0700, Eric Lavigne <la**********@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
Aug 21 '05 #2
>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).

Aug 21 '05 #3
On Sun, 21 Aug 2005 13:41:14 -0400, Eric Lavigne <la**********@gmail.com>
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
Aug 21 '05 #4
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.

Aug 21 '05 #5
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.

Aug 21 '05 #6
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.
Aug 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bill | last post by:
I am passing a string of text between a page and a database. It is a book title, which consists of 2 or three words, and only the first word is passing to the database. The page that passes the...
15
by: Ridimz | last post by:
When use ifstream, how do I ignore the last line of a file if it doesn't contain any information? Thanks in advance, Ridimz
14
by: nic977 | last post by:
I am asked to write a simple program to displays the last n lines from a given text file. But I have no ideas how C defines a "line" in a text file. How does it tell if it is the end of the line,...
4
by: Charles Erwin | last post by:
Is there any way, upon scanning in a file line by line to avoid missing the last line if there is not a newline character (aka you have to hit return on the last line of input in your file). I was...
1
by: Patrick | last post by:
Hi All, Can someone take a look at my script and tell me if I am missing something as to why the last line of data from my input file is being written twice in the echo. I have included a short...
7
by: DemonWasp | last post by:
I've been having some trouble getting the Scanner class to operate the way I'd like. I'm doing some fairly basic file IO and I can't seem to get the class to load the last line/token any way I try....
6
by: magix | last post by:
Hi, when I read entries in file i.e text file, how can I determine the first line and the last line ? I know the first line of entry can be filtered using counter, but how about the last line...
23
by: Florian Lindner | last post by:
Hello, can I determine somehow if the iteration on a list of values is the last iteration? Example: for i in : if last_iteration: print i*i else:
8
by: Adam Sandler | last post by:
Hello, I'm trying to input a txt file. Each line in the text file contains a discrete code or value -- so I want to populate the an arraylist with each line. I've noticed a problem. The code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.