By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,450 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

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

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

Expand|Select|Wrap|Line Numbers
  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 =, # 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()
The test program that call this is simple

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

Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\boa-constructor\test of snake\", line 4, in ?
csoundroutines.get_instr('bay-at-night.csd', 'test.orc', 1)
File "C:\Python24\Lib\site-packages\boa-constructor\test of snake\", line 18, in get_instr
cmd =, # so by the time we get here, we're ready
Sep 30 '06 #1
Share this Question
Share on Google+
3 Replies

Expert 5K+
P: 6,596
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

do stuff
except(error you want to catch)

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

P: 46
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

Expert 5K+
P: 6,596
OK, here's a hint

Expand|Select|Wrap|Line Numbers
  1.         try:
  2.             while True:
  3.                 cmd =, # 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'"
Sep 30 '06 #4

Post your reply

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