newbie help

Stephen Thorne stephen.thorne at gmail.com
Wed Mar 23 23:01:46 EST 2005


On 23 Mar 2005 16:05:58 -0800, ashtonn at gmail.com <doodle4 at gmail.com> wrote:
> I want to pass and index number  and a file name from the command line.
> This index number corresponds to a buffer. I need to maintain 2 such
> buffers for my test.
> 
> In C you would do someting like this
> unsigned char buf1[512];
> 
> In python is this as simple as?
> buf 1 = 512
> buf 2 = 512
> And how do i keep track of buf1 and buf2
> Thanks.

Okay, I've been reading some of your posts in the last few weeks, and
it seems we're still not past the "Python doesn't treat memory like C
does" meme.

I *think* what your C code is doing is creating a 512 byte long
unsigned char containing increasing values. i.e. buf[0] == 0, buf[1]
== 2, etc.

I have no idea why, but you get that.

Anyway, in Python, the way we create a list containing values like
that is with a library function called 'range'

range(10) = [0,1,2,3,4,5,6,7,8,9]

range(1,11) = [1,2,3,4,5,6,7,8,9,10]

range(10,0,-1) = [10,9,8,7,6,5,4,3,2,1]

So in order to create a list (a list is python object,
http://docs.python.org/tut/node5.html#SECTION005140000000000000000 for
more information) that contains the numbers from 0 to 511, you would
do.

mylist = range(512)

As for your other, implied, question, about passing a filename and a
number to your program, you would do this:

import sys 
filename = sys.argv[1]
code = int(sys.argv[2])


So, for instance, a program which writes the numbers from 0 to 511 to
a file, as passed on the commandline, would probably look like this:

@ import sys
@
@ filename = sys.argv[1]
@ code = int(sys.argv[2])
@
@ if code == 1:
@     f = file(filename, 'w')
@     for x in range(512):
@          f.write("%d\n" % x)
@    f.close()

(The @ marks are to preserve the indentation on usenet, I believe
google strips them).

I hope that helps you.
-- 
Stephen Thorne
Development Engineer



More information about the Python-list mailing list