Opening file problem in wxWindows program
Fredrik Lundh
effbot at telia.com
Thu Sep 14 11:08:11 EDT 2000
David Ishee wrote:
> I am writing a program that uses wxWindows for the GUI in Windows NT
> with Python 1.5.2. I am trying to open a file and read the contents
> within this program and I'm having trouble. I have the statment:
>
> usernameFile = open('usernames.dat', 'r')
>
> However, I get an error message saying:
>
> TypeError: illegal argument type for built-in operation
>
> If I create a blank test script with this statement as the only line,
> it works fine. If I enter the statement in the command line of the
> interpreter, it works fine. What could be the problem with using it in
> a script interspersed with wxWindows stuff?
>
> I have imported these at the top of my script:
>
> from wxPython.wx import *
note that from-import imports *all* functions and
names from the module:
- maybe wxPython.wx contains an open function?
> from os import *
- maybe os contains an open function? (it does)
> from string import *
- maybe string contains an open function?
> Any ideas?
just follow the Import Rule:
"Always use import.
As usual, there are a number of exceptions to this rule:
1. The Module Documentation Tells You To Use from-import.
2. You're Importing a Package Component.
3. You Don't Know the Module Name Before Execution.
4. You Know Exactly What You're Doing."
from http://www.pythonware.com/people/fredrik/fyi/fyi06.htm
(see the webpage for more info)
looks like you broke rule four ;-)
:::
printing the doc string can also help:
>>> print open.__doc__
open(filename[, mode[, buffering]]) -> file object
Open a file. The mode can be 'r', 'w' or 'a' for reading (default),
writing or appending. The file will be created if it doesn't exist
when opened for writing or appending; it will be truncated when
opened for writing. Add a 'b' to the mode for binary files.
Add a '+' to the mode to allow simultaneous reading and writing.
If the buffering argument is given, 0 means unbuffered, 1 means line
buffered, and larger numbers specify the buffer size.
>>> from os import *
>>> print open.__doc__
open(filename, flag [, mode=0777]) -> fd
Open a file (for low level IO).
</F>
<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list