468,765 Members | 1,279 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Linux > python > file-I/O ?

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 :
f=open('/tmp/workfile', 'w')
print f <open file '/tmp/workfile', mode 'w' at 80a0960> <-- OK

But: f.read(size)

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.
Dec 24 '05 #1
4 3241
n...@absamail.co.za wrote:
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 :
f=open('/tmp/workfile', 'w')
print f <open file '/tmp/workfile', mode 'w' at 80a0960> <-- OK

But: f.read(size)

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

Dec 24 '05 #2
On 2005-12-24, ne**@absamail.co.za <ne**@absamail.co.za> wrote:
f.read(size)
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

--
Grant Edwards grante Yow! My Aunt MAUREEN was
at a military advisor to IKE &
visi.com TINA TURNER!!
Dec 24 '05 #3

ne**@absamail.co.za wrote:
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 :
f=open('/tmp/workfile', 'w')
print f <open file '/tmp/workfile', mode 'w' at 80a0960> <-- OK

But: f.read(size)

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

Dec 24 '05 #4
ne**@absamail.co.za wrote:
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 :
f=open('/tmp/workfile', 'w')
print f <open file '/tmp/workfile', mode 'w' at 80a0960> <-- OK

But: f.read(size)
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
Dec 25 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Logan | last post: by
5 posts views Thread by Fausto Arinos Barbuto | last post: by
1 post views Thread by Sorin Schwimmer | last post: by
3 posts views Thread by Paddy | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.