Linux > python > file-I/O ?

N

news

I've just started to test/learn python.
I've got Linux > mandrake9 > python & documentation.
What I'll initially want to be doing needs file I/O, so I
wanted to confirm file I/O early in my tests.

Following the examples :<open file '/tmp/workfile', mode 'w' at 80a0960> <-- OK

But:Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'size' is not defined <-- ?? Obj-method unknown ??


What's wrong ?

I read: "The set of such modules is a configuration
option which also depends on the underlying platform."
My documenation refers also to Mac & Win installations.
Is there a linux > python NewsGroup ?

Thanks for any info.

== Chris Glur.
 
D

David Wahler

I've just started to test/learn python.
I've got Linux > mandrake9 > python & documentation.
What I'll initially want to be doing needs file I/O, so I
wanted to confirm file I/O early in my tests.

Following the examples :
<open file '/tmp/workfile', mode 'w' at 80a0960> <-- OK

But:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'size' is not defined <-- ?? Obj-method unknown ??

The expression "f.read(size)" means: take the object referred to by "f"
(a variable), and call the method named "read" with a single parameter,
determined by the expression "size". Since you haven't declared a
variable called size, Python has no idea what you're talking about. You
have to either assign a value to size, or pass in a number directly, as
in "f.read(1000)". You can also call the read method with no parameters
to read the entire contents of the file.

Even if you do this, you'll still have problems because passing the
parameter "w" to the open function means that you're opening the file
in write-only mode. If you want to read from it, you'll need to use
"r", "r+" or "w+" instead.

Hope this helps.

-- David
 
G

Grant Edwards

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'size' is not defined <-- ?? Obj-method unknown ??


What's wrong ?

You haven't defined anything named "size".

Assuming you want to read 1024 bytes, try:

size =1024
data = f.read(size)

or, equivalently

data = f.read(1024)
I read: "The set of such modules is a configuration
option which also depends on the underlying platform."

OK. Do you have a question regarding that sentence?
My documenation refers also to Mac & Win installations.

You are correct.
Is there a linux > python NewsGroup ?

Yes: comp.lang.pythong
 
G

gene tani

I've just started to test/learn python.
I've got Linux > mandrake9 > python & documentation.
What I'll initially want to be doing needs file I/O, so I
wanted to confirm file I/O early in my tests.

Following the examples :
<open file '/tmp/workfile', mode 'w' at 80a0960> <-- OK

But:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'size' is not defined <-- ?? Obj-method unknown ??


What's wrong ?

I read: "The set of such modules is a configuration
option which also depends on the underlying platform."
My documenation refers also to Mac & Win installations.
Is there a linux > python NewsGroup ?

Thanks for any info.

== Chris Glur.

i don't think there's a specific list for linux, only for mac and win32
http://mail.python.org/mailman/listinfo

if you're wanting file size, you need to os.stat()
http://docs.python.org/lib/os-file-dir.html
 
S

Steve Horsley

I've just started to test/learn python.
I've got Linux > mandrake9 > python & documentation.
What I'll initially want to be doing needs file I/O, so I
wanted to confirm file I/O early in my tests.

Following the examples :
<open file '/tmp/workfile', mode 'w' at 80a0960> <-- OK

But:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'size' is not defined <-- ?? Obj-method unknown ??


What's wrong ?

size should be a number of bytes to read. E.g.:
f.read(1000)
would read 1000 bytes. The size is optional:
f.read()
would read the entire file in one hit. Beware doing this on huge
files that could run you out of memory.

I read: "The set of such modules is a configuration
option which also depends on the underlying platform."
My documenation refers also to Mac & Win installations.
Is there a linux > python NewsGroup ?
Not that I know of. Python is much the same whatever platform it
is on. The problem you see above would be exactly the same on
Linux, Windows or any other O/S.

Just remember to say what O/S when you post problems, just in
case it's relevant.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,368
Members
48,060
Latest member
JerrodSimc

Latest Threads

Top