473,795 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3433
n...@absamail.c o.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(siz e)" 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(100 0)". 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.c o.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.pytho ng

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

ne**@absamail.c o.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.c o.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
4968
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
3850
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 less - during my own installation of Python 2.3 on Fedora Core 1 Linux for a friend of mine). Anyway, HTH, L.
12
8233
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 not - be able to use non-ASCII characters in strings and in Tk GUI button labels and GUI window titles and in raw_input data without Python returning wrong case in manipulated
5
3359
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 setup.py install" but that did not work. I later found a RPM for Psyco but it wasn't suitable to the Linux version I'm currently using. Has any of you had a similar experience? If so, how did you manage to solve the problem (in the case that you...
1
3465
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 tried Python 2.5. I tried with and without the suggested -fwrapv compiler option, and make gave me the same:
1
2168
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 installed the "hugemem" kernel distribute by Red Hat. Yet my Python processes get terminated upon using 4 GB of memory (system has 8 GB). What can be done? Below are some stats about the computer. Thank you for any help. Linux...
1
4278
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 windows and linux by various methods. We are trying to resolve a problem of not being able to connect to the linux box from the xp machines using python XMLRPC. (This is the module used in the software I am trying to set up) Python is at 2.4...
9
2100
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 directory) with a 100% reliable method... My goal is to verify if an/several extension(s) are installed and to automatically install the missing ones on Linux or Win32.
3
11290
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 help would be appreciated: dmccarthy: file /opt/questasim_6.4/questasim/linux/libucdb.a /opt/
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.