469,366 Members | 2,286 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

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
  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()
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\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
Sep 30 '06 #1
3 1291
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

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
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
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'"
Sep 30 '06 #4

Post your reply

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

Similar topics

1 post views Thread by IndianOZ | last post: by
2 posts views Thread by Nobody | last post: by
11 posts views Thread by Michael B. | last post: by
6 posts views Thread by scottyman | last post: by
9 posts views Thread by Wayne Smith | last post: by
reply views Thread by Frederic Rentsch | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.