473,734 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

46 New Member
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\Li b\site-packages\boa-constructor\tes t of snake\test_of_c soundroutines.p y", line 4, in ?
csoundroutines. get_instr('bay-at-night.csd', 'test.orc', 1)
File "C:\Python24\Li b\site-packages\boa-constructor\tes t of snake\csoundrou tines.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 1599
bartonc
6,596 Recognized Expert Expert
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
eric dexter
46 New Member
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 Recognized Expert Expert
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
8000
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 based) with the following code: InputStream in = client.getInputStream(); BufferedImage rimg = ImageIO.read(in); File file = new File("d:\\ajay.jpg");
11
3762
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 2003 Server or ADO or ODBC issue, I am posting this on all of the three newsgroups. That's the setup: Windows 2003 Server with IIS and ASP.NET actiavted Access 2002 mdb file (and yes, proper rights are set on TMP paths and path,
2
356
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 a static .lib version so he could give a customer a single .exe file for the "free version". Certain parts of my library are required to live inside a DLL.
11
2134
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 loading procedure there's a point or two where I'll malloc a struct which I then don't need, and dispose of it; I was wondering if there was some way I could optimize this kludge out of existence: void loadContacts () { int fd, bytesread; if (...
6
4896
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 the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
9
3833
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 site for a small club I belong to and one of the features I would like to include is the ability to allow users to upload image files. unfortunately the servers web root www folder only allows READ and EXECUTE permissions, which makes it...
10
1609
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 people like this!) I have a bunch of look-up tables to store information I'll have the users choose form drop-down boxes (aides - hearing aid, cain, dentures, etc) (Language the client speaks: English, Spanish, Russian, etc). I want the...
0
1052
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 "<pyshell#62>", line 1, in -toplevel-
1
2723
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 problem is that the server code can't read the httpfilecollection. the count is always zero. my upload form's form tag looks like this:
0
8951
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8783
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9458
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9248
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9187
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6743
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3266
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2184
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.