473,324 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 3404
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: leroybt.rm | last post by:
I don't understand why this does not work: <FILE1> test1.py #Import Packages import string # data=0 data=data+1
4
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or...
12
by: Mike Dee | last post by:
A very very basic UTF-8 question that's driving me nuts: If I have this in the beginning of my Python script in Linux: #!/usr/bin/env python # -*- coding: UTF-8 -*- should I - or should I...
5
by: Fausto Arinos Barbuto | last post by:
Hi All; I have Psyco (on Windows XP) and now I want to install it on Linux, too. I FTP'd the tarball (tar.gz) from Psyco's site but can't get it compiled. First, I tried the usual "python...
1
by: Sorin Schwimmer | last post by:
Hi All, After a disaster in which I lost my whole harddrive, I decided to install the newest everything that I use. I put the latest Gentoo Linux, with gcc 4.1.1, installed tcl/tk 8.4.14 and...
1
by: metaglossary | last post by:
I'd like use more than 4 GB of memory for a single python process. Is this possible with a 64-bit processor? I'm using a Woodcrest processor, which I presume supports 64-bit addressing. I've...
1
by: getafixx | last post by:
Hello everyone, We have a linux server (Fedora core 7, default install, firewall turned off) and a bunch of windows XP machines on network/domain. All machines are visible and I can get to both...
9
by: pythonewbie | last post by:
Hi all, I am newbie in Python, my wish would be to create python applications for both Linux/Win32. I am stucked on creating a function to get the Python install directory (and site-packages...
3
by: Paddy | last post by:
Hi, I am am falling at the first hurdle when trying to access a library using ctypes. I have a file libucdb.so which the file command says is shared object, but I cannot get it to load: Any...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.