FileDialog Question(s)

I hope this question isn't beyond the scope of this group. If it is, I apologize. I'm new not only to wxPython but Python & Programming in general. I began learning Programming/Python roughly 3 months ago.

I understand basically what's going on with the code below, however I'm a little confused about the lines of code after the FileDialog constructor. Lines (6-10) are where my questions lay. . .I understand that line 4 opens the file open window. Line 5's if statement I understand. Line 6,7,8,9 I don't really get, and was hoping someone could explain to me what each of these lines is doing??? Thank You for ANY help!

GetFilename() returns the current file name. .
GetDirectory() returns the current working directory. .

But how are we using these methods in the code? Why are they there, and what exactly are they doing??

Then line 8 I don't get at all. .
And line 9, I'm completely lost. .

    1 def OnOpen(self,e):
    2 """ Open a file"""
    3 self.dirname = ''
    4 dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
    5 if dlg.ShowModal() == wx.ID_OK:
    6 self.filename = dlg.GetFilename()
    7 self.dirname = dlg.GetDirectory()
    8 f = open(os.path.join(self.dirname, self.filename), 'r')
    9 self.control.SetValue(f.read())
   10 f.close()
   11 dlg.Destroy()

I hope this question isn't beyond the scope of this group\. If it is,

I apologize. I'm new not only to wxPython but Python & Programming in
general. I began learning Programming/Python roughly 3 months ago.

I understand basically what's going on with the code below, however I'm
a little confused about the lines of code after the FileDialog
constructor. Lines (6-10) are where my questions lay. . .I understand
that line 4 opens the file open window. Line 5's if statement I
understand. Line 6,7,8,9 I don't really get, and was hoping someone
could explain to me what each of these lines is doing??? Thank You for
ANY help!

GetFilename() returns the current file name. .
GetDirectory() returns the current working directory. .

But how are we using these methods in the code? Why are they there, and
what exactly are they doing??

Then line 8 I don't get at all. .
And line 9, I'm completely lost. .

1     def OnOpen\(self,e\):
2         """ Open a file"""
3         self\.dirname = ''
4         dlg = wx\.FileDialog\(self, "Choose a file", self\.dirname,

"", "*.*", wx.OPEN)
5 if dlg.ShowModal() == wx.ID_OK:
6 self.filename = dlg.GetFilename()
7 self.dirname = dlg.GetDirectory()
8 f = open(os.path.join(self.dirname, self.filename), 'r')
9 self.control.SetValue(f.read())
10 f.close()
11 dlg.Destroy()

self.filename and self.dirname are attributes of self. self is likely
your frame

"self" is an variable name that points to an "instance" of some class;
meaning you've created some object and you are reffering to that
object with the varible name "self". "self" is a variable name
pointing to that instance object.

The line "def OnOpen(self,e)" defines a method. That method, OnOpen(),
is part of a class. "def OnOpen"; The "def" part means "I'm going to
DEFine a method. defs are templates for creating method objects. And
classes are templates for creating instance objects.

Unlike most other languages, Python (and hence wxPython) expects the
first argument of a method "def"inition to be a "instance object" of
the class the method is apart of. Other languages, self/this/me are
implied and usually are not required by the programmer to be
specified. Python (practically) requires it.

There are many many posts on this point, said many different ways. I
tried to use the common terms. But understanding these terms and how
classes and methods are syntactically structured will allow you to
understand lines of code like "self.filename = dlg.GetFilename()"

If you define a class definition like:

class Person(object): # creates a class definition or a "class" named
"Person"

    language = 'gibberish' # a "class" attribute named "language"
that point to a string object 'gibberish'

    def __init__(self, name, age, lost): # creates a method named
"__init__"
        self.name = name
        self.age = age
        longlost = lost # not saved; no "self"
        print name, ' gets lost', longlost

    def show(self): # creates a method named "show" of class Person
        print "Name:", self.name # self.name is an instance
attribute
        print "Age:", self.age # self.age; same also called just
"attribute"

now you can create a whole bunch of instances of "Person"
dp = Person("DevPlayer", 42, 'inspace') # creates an instance
myalias = dp # creates another varible
name; not another Person object
ck = Person("Chris", 111, 'inpython') # creates an instance
rd = Person("Robin", 2011, 'inpheonix') # creates an instance

But how do you show the attributes of each "instance".
# same question different vocabulary
But how do you show values of the attributes of each "instance".

Try:

print dp.name <<< something I typed in the python interperter

DevPlayer

print ck.age

111

rd.show()

Robin
2011

myalias.show()

DevPlayer
42

"myalias" is just another variable name that points to the same
"object" that the varible name "dp" points too.

In Python variable names (aka variables) are like pointers. Variables
do not "HOLD" data. That is an important point. Variables are "names"
that point to data. "self" is a variable name that points to an
object. That object has attributes. An attribute is a name that points
to an object. So "self.filename" is a name, "self", pointing to an
object, probably an instance of wx.Frame() maybe named "frame". And
the frame object needs to save the filename, perhaps something like
"autoexec.bat", or whatever, into an attribute, named "filename" which
is a name.

But how does Person.show() know how to print out dp.name or ck.age?
in Person def show(self), "self" points to the instance object for
"dp", "ck" or "rd". Not the class definition of Person. In
Person.__init__() the variable names "name" and "age" are assigned to
be attributes of each instance dp, ck and rd when they are created.

So self.name could be dp.name or ck.name or rd.name.

first: methods are like functions.
"dlg" is a varable name that points to a "dialog box instance object"
and "GetFilename" is a method name defined in wx.DialogBox and added
to the dialog box instance. You've reffered to your dialog box
instance with the variable name "dlg".

The line "self.filename = dlg.GetFilename()"
   means assign the value, probably a string object like
"autoexec.bat",

      into the attribute named "filename"

      of the current object instance, named "self",
      which is probably a frame or window,

   which is returned from the method named "GetFileName",
   which is a method in the object, a dialogbox,
      referred to by the variable name "dlg",

···

On Nov 25, 11:26 pm, Chris Kavanagh <cka...@gmail.com> wrote:

f = open(os.path.join(self.dirname, self.filename), 'r')

open( filename, mode )
   open() returns a file object
   a file object is not the content nor the filename. a file object
has all sorts of attributes. mainly a filehandle.
   "f" is a varible name that stands for "a File object"

Python as a builtin object called "file". It is bad practice to make
your own variable name to be the same as what other Python programmers
expect to be a certain type of object. In this case "file" is a very
common one. so the examples you've pulled that from just use the
letter "f"

the quote R quote is the mode of how the file is open. it stands for
read-only; "open()'s" mode argument is a string of either 'w', 'r',
'w+' and others.

os.path.join(self.dirname, self.filename)

os is a standard library
path is a nice python module that comes as part of the "os" package.
It works with the syntax of a filename for many operating systems as
well as some other things like file timestamps and stuff.

self.dirname is a string for a folder or directory, like "C:\Documents
and Settings\User\My Documents"
self.filename probably points string for a filename like "MyExpenses.xls"

join() is a function that makes puts together a list of strings into
one string, inserting slash or backslashes as needed.
it joins a bunch of strings into one longer string.

so that line you see is
variable_named_F equals the_return_value_from_the_open-file-object_function(
     concatenate_list_of_strings_function(
          folder_string, filename_string),
     file_mode = 'open_file_in_read_only_mode'))

And line 9, I'm completely lost. .
9 self.control.SetValue(f.read())

self is the varible name for your frame
control is the varible name for some widget, probably a textctrl or stc control
SetValue is control's method that fills the textCtrl with text (for you to see)

f.read() -
"F" is the variable name for your file object that you opened with the
"open()" function
read() is a method of a file object that reads the contents of the
file from the hard drive into memory

so to restate:

set the value of the widget named "control", a widget in my frame,
named "self", with all the data from the file named "F"

or resaid
put file F's content into my wx.TextCtrl so I can see it.

Sounds like you might benefit from getting into an interpreter that
displays nice help
type this to run the wxPython version of Python's interpreter "shell"

pysliceshell
or
pycrust

then import some libraries/packages to check out; like;
import wx
import os
import sys

type
wx.Frame(
and see the help pop up

Dev and All,

Thanks very much for this explanation.

Perhaps Chris is in a similar boat as me.

I came to Python from MS BASIC and Pascal and Fortran and never had
any formal training in Object-Oriented programming. I picked up bits
and pieces over the years, but I still do a lot of my thinking in
Python via that old terminology. I can stumble around in C++ and make
simple modifications to other people's code, but what's really going
on is a bit of a mystery in many cases.

Perhaps the language I use below isn't correct, but maybe it'll help
Chris get a little more basic understanding.

1) Python routines generally limit the "scope" of variables. If you
define a routine (via "def ..."), then the variables in that routine
will only be available inside that routine unless you explicitly tell
Python to make them available to other routines. In BASIC you do that
by passing the variable in and out when you call the routine or by
defining a COMMON block. I think of "self" as being kinda-sorta
similar to a COMMON block (ignoring the details of passing "by value"
or "by reference" and "pointers" and so forth). You use it to pass
values and variables and so forth out of your routines into other
parts of the program. You can pass "self" in and out of your routines
when you "def" them.

2) Python and wxPython use Object-Oriented constructs. The
"dot-something" notation means "attach this value/property to this
variable/function/object". So if I have a variable in my routine
called "myName" then "myName.Last = 'Obama' " says attach a property
called "Last" to the variable "myName" and give it the value "Obama".
Lots of the workings of Python and wxPython depend on understanding
this at a rudimentary level at least. Having variables and so-forth
be "objects" rather than just static storage has lots of advantages
but takes a little getting used to. E.g. When you want to label a
Button, you call the appropriate property of the Button to change the
label value via the "dot" notation.

HTH a little.

Corrections welcome.

Cheers,
Scott.

···

On Sat, Nov 26, 2011 at 1:07 AM, DevPlayer <devplayer@gmail.com> wrote:

[...]

I haven’t read this tread all the way through and I am very new to programming period only been at it a year with zero programming experience prior… so I am doing this more as an intellual excerice … Line 7( f = open(os.path.join(self.dirname, self.filename), ‘r’)) means the variable who"s name if “f” equals the value of (variable) self.dirname and (variable)self.file(these two bits of info is the location of the file you are looking for… os.path is part of the os library which houses that function… the .join tells joins the dirname and file name into something like c:\file_your_looking_for… then we are telling it to read this information"r" at that location… line 9 self.control.SetValue(f.read()) line 9 says the variable self.control that value is going to be set to f.read… that means open the f variable , read it to get the value in it and that is what self.control’s value is set to… and line 10 learned the hard way closes the f variable file or connection… trust me always do that it will reek havoc with your code if you don’t… Sorry to everyone for this I’m just testing myself to see how much I understand what I have been learning since I am uber new to python and programming

···

On Fri, Nov 25, 2011 at 8:26 PM, Chris Kavanagh ckava3@gmail.com wrote:

I hope this question isn’t beyond the scope of this group. If it is, I apologize. I’m new not only to wxPython but Python & Programming in general. I began learning Programming/Python roughly 3 months ago.

I understand basically what’s going on with the code below, however I’m a little confused about the lines of code after the FileDialog constructor. Lines (6-10) are where my questions lay. . .I understand that line 4 opens the file open window. Line 5’s if statement I understand. Line 6,7,8,9 I don’t really get, and was hoping someone could explain to me what each of these lines is doing??? Thank You for ANY help!

GetFilename() returns the current file name. .

GetDirectory() returns the current working directory. .

But how are we using these methods in the code? Why are they there, and what exactly are they doing??

Then line 8 I don’t get at all. .

And line 9, I’m completely lost. .

1 def OnOpen(self,e):

2 “”" Open a file"“”

3 self.dirname = ‘’

4 dlg = wx.FileDialog(self, “Choose a file”, self.dirname, “”, “.”, wx.OPEN)

5 if dlg.ShowModal() == wx.ID_OK:

6 self.filename = dlg.GetFilename()

7 self.dirname = dlg.GetDirectory()

8 f = open(os.path.join(self.dirname, self.filename), ‘r’)

9 self.control.SetValue(f.read())

10 f.close()

11 dlg.Destroy()

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Thanks so much for your help! I greatly appreciate you taking the time to explain that to me. . .

I didn't do a very good job of asking the question(s), though. I should have told you guys that I completely understand the concepts of Object Oriented Programming. The meaning of 'self' and methods (functions inside a class) ect,I get. I just didn't quite understand what was going on in those particular lines of code. I think the email from Clay Richmond (although I haven't finished reading it) is basically answering my questions.

Again, I can't thank you enough for taking the time to explain all that to me. I just didn't do a good job of asking my question.

                                                    Thanks,
                                                            Chris

···

On 11/26/2011 10:47 AM, grunculus wrote:

Dev and All,

Thanks very much for this explanation.

Perhaps Chris is in a similar boat as me.

I came to Python from MS BASIC and Pascal and Fortran and never had
any formal training in Object-Oriented programming. I picked up bits
and pieces over the years, but I still do a lot of my thinking in
Python via that old terminology. I can stumble around in C++ and make
simple modifications to other people's code, but what's really going
on is a bit of a mystery in many cases.

Perhaps the language I use below isn't correct, but maybe it'll help
Chris get a little more basic understanding.

1) Python routines generally limit the "scope" of variables. If you
define a routine (via "def ..."), then the variables in that routine
will only be available inside that routine unless you explicitly tell
Python to make them available to other routines. In BASIC you do that
by passing the variable in and out when you call the routine or by
defining a COMMON block. I think of "self" as being kinda-sorta
similar to a COMMON block (ignoring the details of passing "by value"
or "by reference" and "pointers" and so forth). You use it to pass
values and variables and so forth out of your routines into other
parts of the program. You can pass "self" in and out of your routines
when you "def" them.

2) Python and wxPython use Object-Oriented constructs. The
"dot-something" notation means "attach this value/property to this
variable/function/object". So if I have a variable in my routine
called "myName" then "myName.Last = 'Obama' " says attach a property
called "Last" to the variable "myName" and give it the value "Obama".
Lots of the workings of Python and wxPython depend on understanding
this at a rudimentary level at least. Having variables and so-forth
be "objects" rather than just static storage has lots of advantages
but takes a little getting used to. E.g. When you want to label a
Button, you call the appropriate property of the Button to change the
label value via the "dot" notation.

HTH a little.

Corrections welcome.

Cheers,
Scott.

On Sat, Nov 26, 2011 at 1:07 AM, DevPlayer<devplayer@gmail.com> wrote:

[...]

Dev, thank you so much for taking the time to explain all that to me (all 4 emails!). I guess I should have said I understand the concepts of OOP, but just didn't quite understand what was going on in the lines of code I mentioned.

I haven't had a chance to thoroughly read all your emails, but I think the others sorta explain what I was actually asking. I'm going to re-read them in the morning when I'm not so tired.

Again, THANK YOU and Grunculus for taking the time to explain what you did. Most people wouldn't have gone so far out of there way to help me!

              Thanks,
                Chris

···

On 11/26/2011 1:07 AM, DevPlayer wrote:

On Nov 25, 11:26 pm, Chris Kavanagh<cka...@gmail.com> wrote:

     I hope this question isn't beyond the scope of this group. If it is,
I apologize. I'm new not only to wxPython but Python& Programming in
general. I began learning Programming/Python roughly 3 months ago.

I understand basically what's going on with the code below, however I'm
a little confused about the lines of code after the FileDialog
constructor. Lines (6-10) are where my questions lay. . .I understand
that line 4 opens the file open window. Line 5's if statement I
understand. Line 6,7,8,9 I don't really get, and was hoping someone
could explain to me what each of these lines is doing??? Thank You for
ANY help!

GetFilename() returns the current file name. .
GetDirectory() returns the current working directory. .

But how are we using these methods in the code? Why are they there, and
what exactly are they doing??

Then line 8 I don't get at all. .
And line 9, I'm completely lost. .

     1 def OnOpen(self,e):
     2 """ Open a file"""
     3 self.dirname = ''
     4 dlg = wx.FileDialog(self, "Choose a file", self.dirname,
"", "*.*", wx.OPEN)
     5 if dlg.ShowModal() == wx.ID_OK:
     6 self.filename = dlg.GetFilename()
     7 self.dirname = dlg.GetDirectory()
     8 f = open(os.path.join(self.dirname, self.filename), 'r')
     9 self.control.SetValue(f.read())
    10 f.close()
    11 dlg.Destroy()

self.filename and self.dirname are attributes of self. self is likely
your frame

"self" is an variable name that points to an "instance" of some class;
meaning you've created some object and you are reffering to that
object with the varible name "self". "self" is a variable name
pointing to that instance object.

The line "def OnOpen(self,e)" defines a method. That method, OnOpen(),
is part of a class. "def OnOpen"; The "def" part means "I'm going to
DEFine a method. defs are templates for creating method objects. And
classes are templates for creating instance objects.

Unlike most other languages, Python (and hence wxPython) expects the
first argument of a method "def"inition to be a "instance object" of
the class the method is apart of. Other languages, self/this/me are
implied and usually are not required by the programmer to be
specified. Python (practically) requires it.

There are many many posts on this point, said many different ways. I
tried to use the common terms. But understanding these terms and how
classes and methods are syntactically structured will allow you to
understand lines of code like "self.filename = dlg.GetFilename()"

If you define a class definition like:

class Person(object): # creates a class definition or a "class" named
"Person"

     language = 'gibberish' # a "class" attribute named "language"
that point to a string object 'gibberish'

     def __init__(self, name, age, lost): # creates a method named
"__init__"
         self.name = name
         self.age = age
         longlost = lost # not saved; no "self"
         print name, ' gets lost', longlost

     def show(self): # creates a method named "show" of class Person
         print "Name:", self.name # self.name is an instance
attribute
         print "Age:", self.age # self.age; same also called just
"attribute"

now you can create a whole bunch of instances of "Person"
dp = Person("DevPlayer", 42, 'inspace') # creates an instance
myalias = dp # creates another varible
name; not another Person object
ck = Person("Chris", 111, 'inpython') # creates an instance
rd = Person("Robin", 2011, 'inpheonix') # creates an instance

But how do you show the attributes of each "instance".
# same question different vocabulary
But how do you show values of the attributes of each "instance".

Try:

print dp.name<<< something I typed in the python interperter

DevPlayer

print ck.age

111

rd.show()

Robin
2011

myalias.show()

DevPlayer
42

"myalias" is just another variable name that points to the same
"object" that the varible name "dp" points too.

In Python variable names (aka variables) are like pointers. Variables
do not "HOLD" data. That is an important point. Variables are "names"
that point to data. "self" is a variable name that points to an
object. That object has attributes. An attribute is a name that points
to an object. So "self.filename" is a name, "self", pointing to an
object, probably an instance of wx.Frame() maybe named "frame". And
the frame object needs to save the filename, perhaps something like
"autoexec.bat", or whatever, into an attribute, named "filename" which
is a name.

But how does Person.show() know how to print out dp.name or ck.age?
in Person def show(self), "self" points to the instance object for
"dp", "ck" or "rd". Not the class definition of Person. In
Person.__init__() the variable names "name" and "age" are assigned to
be attributes of each instance dp, ck and rd when they are created.

So self.name could be dp.name or ck.name or rd.name.

first: methods are like functions.
"dlg" is a varable name that points to a "dialog box instance object"
and "GetFilename" is a method name defined in wx.DialogBox and added
to the dialog box instance. You've reffered to your dialog box
instance with the variable name "dlg".

The line "self.filename = dlg.GetFilename()"
    means assign the value, probably a string object like
"autoexec.bat",

       into the attribute named "filename"

       of the current object instance, named "self",
       which is probably a frame or window,

    which is returned from the method named "GetFileName",
    which is a method in the object, a dialogbox,
       referred to by the variable name "dlg",

Thank you Clay for your answer. Looks good to me,lol. . .Seriously, I understand what you're saying on those two lines, but. . .

Ok, so line 4 opens the dialog box, line 5 says if they click ok then we'll continue with lines 6 - 10. Line 6 sets self.filename equal to whatever the method dlg.GetFilename() returns, right? But what does it return, is my question? What File name is it getting? The one the user chooses from the dialog box?? And I have the same question for the next line too. I understand we're setting self.dirname equal to whatever the method dlg.GetDirectory() returns. I'm just not sure what it returns? I know what the documents say, "returns the current working directory". But WHAT current working directory??

I hope this clarifies my questions a little better. I know this should be easy to figure out, maybe I'm just too tired right now, lol. But I'm just not sure what those two lines are ACTUALLY doing.

            Thanks In Advance,
              Chris

···

On 11/26/2011 1:57 PM, Clay Richmond wrote:

I haven't read this tread all the way through and I am very new to
programming period only been at it a year with zero programming
experience prior... so I am doing this more as an intellual excerice ...
Line 7( f = open(os.path.join(self.dirname, self.filename), 'r')) means
the variable who"s name if "f" equals the value of (variable)
self.dirname and (variable)self.file(these two bits of info is the
location of the file you are looking for... os.path is part of the os
library which houses that function... the .join tells joins the dirname
and file name into something like c:\file_your_looking_for.... then we
are telling it to read this information"r" at that location... line 9
self.control.SetValue(f.read()) line 9 says the variable self.control
that value is going to be set to f.read... that means open the f
variable , read it to get the value in it and that is what
self.control's value is set to... and line 10 learned the hard way
closes the f variable file or connection... trust me always do that it
will reek havoc with your code if you don't.... Sorry to everyone for
this I'm just testing myself to see how much I understand what I have
been learning since I am uber new to python and programming

On Fri, Nov 25, 2011 at 8:26 PM, Chris Kavanagh <ckava3@gmail.com > <mailto:ckava3@gmail.com>> wrote:

       I hope this question isn't beyond the scope of this group. If it
    is, I apologize. I'm new not only to wxPython but Python &
    Programming in general. I began learning Programming/Python roughly
    3 months ago.

    I understand basically what's going on with the code below, however
    I'm a little confused about the lines of code after the FileDialog
    constructor. Lines (6-10) are where my questions lay. . .I
    understand that line 4 opens the file open window. Line 5's if
    statement I understand. Line 6,7,8,9 I don't really get, and was
    hoping someone could explain to me what each of these lines is
    doing??? Thank You for ANY help!

    GetFilename() returns the current file name. .
    GetDirectory() returns the current working directory. .

    But how are we using these methods in the code? Why are they there,
    and what exactly are they doing??

    Then line 8 I don't get at all. .
    And line 9, I'm completely lost. .

       1 def OnOpen(self,e):
       2 """ Open a file"""
       3 self.dirname = ''
       4 dlg = wx.FileDialog(self, "Choose a file",
    self.dirname, "", "*.*", wx.OPEN)
       5 if dlg.ShowModal() == wx.ID_OK:
       6 self.filename = dlg.GetFilename()
       7 self.dirname = dlg.GetDirectory()
       8 f = open(os.path.join(self.__dirname,
    self.filename), 'r')
       9 self.control.SetValue(f.read()__)
      10 f.close()
      11 dlg.Destroy()

    --
    To unsubscribe, send email to
    wxPython-users+unsubscribe@__googlegroups.com
    <mailto:wxPython-users%2Bunsubscribe@googlegroups.com>
    or visit http://groups.google.com/__group/wxPython-users?hl=en
    <http://groups.google.com/group/wxPython-users?hl=en&gt;

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Hi Chris,

The wxPython demo is your friend here.

        1 def OnOpen(self,e):
        2 """ Open a file"""
        3 self.dirname = ''
        4 dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
        5 if dlg.ShowModal() == wx.ID_OK:
        6 self.filename = dlg.GetFilename()
        7 self.dirname = dlg.GetDirectory()
        8 f = open(os.path.join(self.__dirname, self.filename), 'r')
        9 self.control.SetValue(f.read()__)
       10 f.close()
       11 dlg.Destroy()

Ok, so line 4 opens the dialog box, line 5 says if they click ok then we'll continue with lines 6 - 10.

correct

Line 6 sets self.filename equal to whatever the method dlg.GetFilename() returns, right? But what does it return, is my question? What File name is it getting? The one the user chooses from the dialog box??

correct

And I have the same question for the next line too. I understand we're setting self.dirname equal to whatever the method dlg.GetDirectory() returns. I'm just not sure what it returns? I know what the documents say, "returns the current working directory". But WHAT current working directory??

I was not sure at all either, so I tried it out in the demo for e.g. FileDialog and add about line 60, just after "WriteText("CWD etc" the following:

         self.log.WriteText("GetDir: %s\n" % dlg.GetDirectory())

Save the changes and run it again (first button).

When user moves in the directory structure the CWD (as returned by os.getcwd()) is changed and that is what is returned by GetDirectory.

Werner

···

On 11/27/2011 04:29 AM, Chris Kavanagh wrote:

# A class method.
    # The DefaultDirectory is preserved between calls (at least on
Windows).
    # It does not correspond to the os.getcwd().
    def OnClick1(self, event):

        # the gui part
        DefaultDirectory = ''
        DefaultFilename = ''
        filter = 'Text files, utf-32-be (*.txt)|*.txt|All files (*.*)|
*.*'
        flags = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST # flags == style
        # instance
        dlg = wx.FileDialog(self, 'Open', DefaultDirectory,\
                DefaultFilename, filter, flags)
        # show dialog and get answer
        r = dlg.ShowModal()
        # interpret the answer
        if r == wx.ID_OK: # "wx" specific
            # a name is selected, path + filename
            FileNameToOpen = dlg.GetPath()
        else:
            # Cancel, esc,
            FileNameToOpen = ''
        dlg.Destroy()

        # pure Python part, FileNameToOpen is used as Python flag
(bool)
        if FileNameToOpen:
            try:
                # fileobj, automatic fileobj closing with included
                # "exception handling"
                with io.open(FileNameToOpen, 'r', encoding='utf-32-
be') as f:
                    AllLines = f.readlines()

                # check, assuming I selected 'a-utf-32-be.txt'
                # <str> or <unicode> depending on the wx-version
                print FileNameToOpen
                # <unicode> and encoding aware display (here cp1252)
                for line in AllLines:
                    print line.rstrip().encode('cp1252', 'replace')
            except Exception: # not good, quick and dirty workaround
                AllLines =
                print 'exception'
            finally:
                pass

Result:

D:\jm\jmpy\textfiles\a-utf-32-be.txt
abc
élève
cœur
Euro

(tanslation, from French into English)
abc
pupil
heart
euro

···

On 26 nov, 05:26, Chris Kavanagh <cka...@gmail.com> wrote:

I hope this question isn&#39;t beyond the scope of this group\.\.\.\.

---

jmf

This makes much more sense to me now Jim. Thanks for taking the time to do this for me!

···

On 11/27/2011 7:54 AM, jmfauth wrote:

On 26 nov, 05:26, Chris Kavanagh<cka...@gmail.com> wrote:

     I hope this question isn't beyond the scope of this group....

     # A class method.
     # The DefaultDirectory is preserved between calls (at least on
Windows).
     # It does not correspond to the os.getcwd().
     def OnClick1(self, event):

         # the gui part
         DefaultDirectory = ''
         DefaultFilename = ''
         filter = 'Text files, utf-32-be (*.txt)|*.txt|All files (*.*)|
*.*'
         flags = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST # flags == style
         # instance
         dlg = wx.FileDialog(self, 'Open', DefaultDirectory,\
                 DefaultFilename, filter, flags)
         # show dialog and get answer
         r = dlg.ShowModal()
         # interpret the answer
         if r == wx.ID_OK: # "wx" specific
             # a name is selected, path + filename
             FileNameToOpen = dlg.GetPath()
         else:
             # Cancel, esc,
             FileNameToOpen = ''
         dlg.Destroy()

         # pure Python part, FileNameToOpen is used as Python flag
(bool)
         if FileNameToOpen:
             try:
                 # fileobj, automatic fileobj closing with included
                 # "exception handling"
                 with io.open(FileNameToOpen, 'r', encoding='utf-32-
be') as f:
                     AllLines = f.readlines()

                 # check, assuming I selected 'a-utf-32-be.txt'
                 #<str> or<unicode> depending on the wx-version
                 print FileNameToOpen
                 #<unicode> and encoding aware display (here cp1252)
                 for line in AllLines:
                     print line.rstrip().encode('cp1252', 'replace')
             except Exception: # not good, quick and dirty workaround
                 AllLines =
                 print 'exception'
             finally:
                 pass

Result:

D:\jm\jmpy\textfiles\a-utf-32-be.txt
abc
�l�ve
c�ur
Euro

(tanslation, from French into English)
abc
pupil
heart
euro

---

jmf