A problem with opening a file

Dieter Maurer dieter at handshake.de
Sun Nov 29 13:34:20 EST 2020


Gabor Urban wrote at 2020-11-29 08:56 +0100:
>I am facing an issue I was not able to solve yet. I have a class saving
>messages to a file. The relevant code is:
>
>----
>
>import OS
>import sys
>
>class MxClass:
>
>    def __init__(self, fName, fMode,....):
>        self.fileName = fName
>        self.fileMode = fMode
>        ....
>
>    def writeMethod(self, message):
>        data = open(self.fileName, self.fileMode)
>        ....
>
>----
>
>Tests reveal: the class is instantiated correctly, other methods work
>perfect. The writeMethod raises a NameError opening the file
>"name 'open' is not defined "

"open" is a so called "built-in" function.
In "normal" (i.e. not restricted) mode, it should be available.
Are you sure, you operate in "normal" mode?

Under Python 3, you could try to import "open" from the module "io".
(Under Python 2, "file" is a synonym for "open").


More information about the Python-list mailing list