stephen.thorne at gmail.com
Thu Mar 24 05:01:46 CET 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;
> In python is this as simple as?
> buf 1 = 512
> buf 2 = 512
> And how do i keep track of buf1 and buf2
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
I *think* what your C code is doing is creating a 512 byte long
unsigned char containing increasing values. i.e. buf == 0, buf
== 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,
more information) that contains the numbers from 0 to 511, you would
mylist = range(512)
As for your other, implied, question, about passing a filename and a
number to your program, you would do this:
filename = sys.argv
code = int(sys.argv)
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
@ code = int(sys.argv)
@ if code == 1:
@ f = file(filename, 'w')
@ for x in range(512):
@ f.write("%d\n" % x)
(The @ marks are to preserve the indentation on usenet, I believe
google strips them).
I hope that helps you.
More information about the Python-list