473,320 Members | 1,853 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,320 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 1588
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 reading the Stream (at the Server which is java...
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 change.... Since I am not sure, if this is a Windows...
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 library (DLL) when suddenly my boss told he wanted...
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 read it back. It works fine, but I notice in my...
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 lost in the Java world. The script at the bottom of...
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 grateful of any help. I'm currently building a web...
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 fascinating activity, and very rewarding to be helping...
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 'abcdefg' Traceback (most recent call last): File...
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 a hidden iframe to a asp.net/vb.net form. the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.