472,353 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

this seems to read file o.k. but doesn't write to a file HELP

This seems to write everything to the shell but not out to a disk file

Expand|Select|Wrap|Line Numbers
  1.  
  2. def get_instr(from_file, to_file, instr_num):
  3.     instr_num_str = str(instr_num)
  4.     out_list = []
  5.     infile = open(from_file, 'r')
  6.     for line in infile: # Internally, for calls the files next() method
  7.         line_list = line.split()
  8.         if len(line_list) > 1 and line_list[1] == instr_num_str:
  9.             out_list.append(line)
  10.             while True:
  11.                 cmd = infile.next(), # so by the time we get here, we're ready
  12.                 cmd_str = cmd[0]
  13.                 print repr(cmd_str)
  14.                 out_list.append(cmd_str)
  15.                 if cmd_str.rstrip() == "endin":
  16.                     break
  17.             break
  18.     infile.close()
  19.     print out_list
  20.     outfile = open(to_file, "w")
  21.     outfile.writelines(out_list)
  22.     outfile.close()
  23.  
  24.  
The test program that call this is simple

Expand|Select|Wrap|Line Numbers
  1.  
  2. csoundroutines.get_instr('bay-at-night.csd', 'test.orc', 1)
  3.  
  4.  
this is the traceback if it means anything

Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\boa-constructor\test of snake\test_of_csoundroutines.py", line 4, in ?
csoundroutines.get_instr('bay-at-night.csd', 'test.orc', 1)
File "C:\Python24\Lib\site-packages\boa-constructor\test of snake\csoundroutines.py", line 18, in get_instr
cmd = infile.next(), # so by the time we get here, we're ready
StopIteration
Sep 30 '06 #1
3 1501
bartonc
6,596 Expert 4TB
When I gave you these line of code, it was as a simple example of was to use the itterative nature of file objects. It needs work to prevent it from breaking when search items are not found. A really cool feature of Python is the

try:
do stuff
except(error you want to catch)
break

I the case of your test, it look like you're not finding the "endin".

The book "learning python" from O'reilly has a hole section on what is a good idea to "wrap" in a try block and what is "over-wrapping". I could not have become the programmer that I am without resources like this, even though python documentation is GREAT and can teach you a lot.
Sep 30 '06 #2
The errors are a little cryptic at times thanks for the help. I am a little dissapointed in the book I bought and now I am bottem fishing for other titles on ebay (my budget being what it is right now). hopefully it becomes as easy as it looks it should be.
Sep 30 '06 #3
bartonc
6,596 Expert 4TB
OK, here's a hint

Expand|Select|Wrap|Line Numbers
  1.         try:
  2.             while True:
  3.                 cmd = infile.next(), # so by the time we get here, we're ready
  4.                 cmd_str = cmd[0]
  5.                 out_list.append(cmd_str)
  6.                 if cmd_str.rstrip() == "endin":
  7.                     break
  8.         except StopIteration:
  9.             print "Did not find 'endin'"
  10.  
Sep 30 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: IndianOZ | last post by:
Hi , I'm trying to send an Image over the Socket Connection (from the Client using winsock, which is a VB based client) and at the other end I'm...
11
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or...
2
by: Nobody | last post by:
Let me start off with a brief overview... This part is not really important, just saying what its for... I had been working on a Windows GUI...
11
by: Michael B. | last post by:
I'm still learning C so I've written a simple app which lets you make a contact list (stored as a linked list of structs), write it to a file, and...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be...
10
by: sara | last post by:
I have been volunteered to write a simple system to help a non-profit enter and track information on the elders they serve. (It's actually a...
0
by: Frederic Rentsch | last post by:
Hi all, Working with read and write operations on a file I stumbled on a complication when writes fail following a read to the end. 30L...
1
by: pbd22 | last post by:
hi. i have been posting this here and elsewhere a lot and can't seem to get resolution on this problem. i have been trying to upload files using...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.