Problem getting a file pathname with tkFileDialog

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cdroulers@hotmail.com

    Problem getting a file pathname with tkFileDialog

    Hello,
    I am working on a school project that requires me to get the path of a
    filename for future treatment.
    I've tried getting a file with tkFileDialog.as kopenfile.


    *************** *************** **************
    import tkFileDialog
    file = tkFileDialog.as kopenfile()
    print file
    *************** *************** **************


    It prints the opened files stuff, but I just can not find how to get
    that path as a string. I've searched around google and the present
    group, and found no documentation on the file class used with
    tkFileDialog. Does someone have a solution for that?

    Thank you

    Christian

  • Tim Daneliuk

    #2
    Re: Problem getting a file pathname with tkFileDialog

    cdroulers@hotma il.com wrote:
    Hello,
    I am working on a school project that requires me to get the path of a
    filename for future treatment.
    I've tried getting a file with tkFileDialog.as kopenfile.
    >
    >
    *************** *************** **************
    import tkFileDialog
    file = tkFileDialog.as kopenfile()
    print file
    *************** *************** **************
    >
    >
    It prints the opened files stuff, but I just can not find how to get
    that path as a string. I've searched around google and the present
    group, and found no documentation on the file class used with
    tkFileDialog. Does someone have a solution for that?
    >
    Thank you
    >
    Christian
    >
    How about:

    print file.name



    --
    ----------------------------------------------------------------------------
    Tim Daneliuk tundra@tundrawa re.com
    PGP Key: http://www.tundraware.com/PGP/

    Comment

    • Sefyroth

      #3
      Re: Problem getting a file pathname with tkFileDialog

      Thanks,

      but I get this error when I try this.

      UnicodeEncodeEr ror: 'ascii' codec can't encode character u'\xe8' in
      position 12: ordinal not in range(128)

      I had encountered it with the askdirectory method as well. Is there an
      easy way to bypass this?

      Thanks again


      Tim Daneliuk wrote:
      cdroulers@hotma il.com wrote:
      Hello,
      I am working on a school project that requires me to get the path of a
      filename for future treatment.
      I've tried getting a file with tkFileDialog.as kopenfile.


      *************** *************** **************
      import tkFileDialog
      file = tkFileDialog.as kopenfile()
      print file
      *************** *************** **************


      It prints the opened files stuff, but I just can not find how to get
      that path as a string. I've searched around google and the present
      group, and found no documentation on the file class used with
      tkFileDialog. Does someone have a solution for that?

      Thank you

      Christian
      >
      How about:
      >
      print file.name
      >
      >
      >
      --
      ----------------------------------------------------------------------------
      Tim Daneliuk tundra@tundrawa re.com
      PGP Key: http://www.tundraware.com/PGP/

      Comment

      • Tim Daneliuk

        #4
        Re: Problem getting a file pathname with tkFileDialog

        Sefyroth wrote:
        Thanks,
        >
        but I get this error when I try this.
        >
        UnicodeEncodeEr ror: 'ascii' codec can't encode character u'\xe8' in
        position 12: ordinal not in range(128)
        >
        I had encountered it with the askdirectory method as well. Is there an
        easy way to bypass this?
        >
        Thanks again
        I believe you are running into a directory or file name that has
        non-ascii characters in it. Python as shipped is set up to
        deal with ascii as its native encoding format. You can change
        this by editing the "site.py" file - look in the Lib directory
        in your python installation. Look for this code:

        -------------------------------
        def setencoding():
        """Set the string encoding used by the Unicode implementation. The
        default is 'ascii', but if you're willing to experiment, you can
        change this."""
        encoding = "ascii" # Default value set by _PyUnicode_Init ()
        if 0:
        # Enable to support locale aware default string encodings.
        import locale
        loc = locale.getdefau ltlocale()
        if loc[1]:
        encoding = loc[1]
        -------------------------------



        Change the "if 0:" to "if 1:" and see if that doesn't fix the problem.



        --
        ----------------------------------------------------------------------------
        Tim Daneliuk tundra@tundrawa re.com
        PGP Key: http://www.tundraware.com/PGP/

        Comment

        • Sefyroth

          #5
          Re: Problem getting a file pathname with tkFileDialog

          Thank you!!! I have had problems with other stuff because of this
          (mainly py2exe!)

          It did the job! I thank you a lot.

          Just wondering though,

          D:/Travaux/5ème session/B51 - Dev. de
          Systèmes/Workspace/LMAOSoft/Controleur.py
          That's my filepath, what is not ASCII in there? è????

          Just checked and it's 138 in ascii... Anyway, thanks a lot

          Christian


          Tim Daneliuk wrote:
          Sefyroth wrote:
          Thanks,

          but I get this error when I try this.

          UnicodeEncodeEr ror: 'ascii' codec can't encode character u'\xe8' in
          position 12: ordinal not in range(128)

          I had encountered it with the askdirectory method as well. Is there an
          easy way to bypass this?

          Thanks again
          >
          I believe you are running into a directory or file name that has
          non-ascii characters in it. Python as shipped is set up to
          deal with ascii as its native encoding format. You can change
          this by editing the "site.py" file - look in the Lib directory
          in your python installation. Look for this code:
          >
          -------------------------------
          def setencoding():
          """Set the string encoding used by the Unicode implementation. The
          default is 'ascii', but if you're willing to experiment, you can
          change this."""
          encoding = "ascii" # Default value set by _PyUnicode_Init ()
          if 0:
          # Enable to support locale aware default string encodings.
          import locale
          loc = locale.getdefau ltlocale()
          if loc[1]:
          encoding = loc[1]
          -------------------------------
          >
          >
          >
          Change the "if 0:" to "if 1:" and see if that doesn't fix the problem.
          >
          >
          >
          --
          ----------------------------------------------------------------------------
          Tim Daneliuk tundra@tundrawa re.com
          PGP Key: http://www.tundraware.com/PGP/

          Comment

          • Tim Daneliuk

            #6
            Re: Problem getting a file pathname with tkFileDialog

            Sefyroth wrote:
            Thank you!!! I have had problems with other stuff because of this
            (mainly py2exe!)
            >
            It did the job! I thank you a lot.
            My pleasure.
            >
            Just wondering though,
            >
            D:/Travaux/5ème session/B51 - Dev. de
            ^
            Systèmes/Workspace/LMAOSoft/Controleur.py
            ^
            I would guess that these are the characters causing the problem.
            Strictly speaking, "ASCII" only goes from 0-127 IIRC with the
            high bit being sort of system dependent (I could be wrong, but
            that seems to tickle something deep in ancient memories).
            That's my filepath, what is not ASCII in there? è????
            >
            Just checked and it's 138 in ascii... Anyway, thanks a lot
            >
            Christian
            >
            >
            Tim Daneliuk wrote:
            >Sefyroth wrote:
            >>Thanks,
            >>>
            >>but I get this error when I try this.
            >>>
            >>UnicodeEncode Error: 'ascii' codec can't encode character u'\xe8' in
            >>position 12: ordinal not in range(128)
            >>>
            >> I had encountered it with the askdirectory method as well. Is there an
            >>easy way to bypass this?
            >>>
            >>Thanks again
            >I believe you are running into a directory or file name that has
            >non-ascii characters in it. Python as shipped is set up to
            >deal with ascii as its native encoding format. You can change
            >this by editing the "site.py" file - look in the Lib directory
            >in your python installation. Look for this code:
            >>
            >-------------------------------
            >def setencoding():
            > """Set the string encoding used by the Unicode implementation. The
            > default is 'ascii', but if you're willing to experiment, you can
            > change this."""
            > encoding = "ascii" # Default value set by _PyUnicode_Init ()
            > if 0:
            > # Enable to support locale aware default string encodings.
            > import locale
            > loc = locale.getdefau ltlocale()
            > if loc[1]:
            > encoding = loc[1]
            >-------------------------------
            >>
            >>
            >>
            >Change the "if 0:" to "if 1:" and see if that doesn't fix the problem.
            >>
            >>
            >>
            >--
            >----------------------------------------------------------------------------
            >Tim Daneliuk tundra@tundrawa re.com
            >PGP Key: http://www.tundraware.com/PGP/
            >

            --
            ----------------------------------------------------------------------------
            Tim Daneliuk tundra@tundrawa re.com
            PGP Key: http://www.tundraware.com/PGP/

            Comment

            • Eric Brunel

              #7
              Re: Problem getting a file pathname with tkFileDialog

              On Wed, 08 Nov 2006 20:01:08 +0100, <cdroulers@hotm ail.comwrote:
              Hello,
              I am working on a school project that requires me to get the path of a
              filename for future treatment.
              I've tried getting a file with tkFileDialog.as kopenfile.
              >
              >
              *************** *************** **************
              import tkFileDialog
              file = tkFileDialog.as kopenfile()
              print file
              *************** *************** **************
              >
              >
              It prints the opened files stuff, but I just can not find how to get
              that path as a string. I've searched around google and the present
              group, and found no documentation on the file class used with
              tkFileDialog. Does someone have a solution for that?
              Use tkFileDialog.as kopenfilename?

              HTH
              --
              python -c "print ''.join([chr(154 - ord(c)) for c in
              'U(17zX(%,5.zmz 5(17l8(%,5.Z*(9 3-965$l7+-'])"

              Comment

              • Eric Brunel

                #8
                Re: Problem getting a file pathname with tkFileDialog

                On Wed, 08 Nov 2006 21:59:38 +0100, Tim Daneliuk <tundra@tundraw are.com>
                wrote:
                Sefyroth wrote:
                >Thanks,
                > but I get this error when I try this.
                > UnicodeEncodeEr ror: 'ascii' codec can't encode character u'\xe8' in
                >position 12: ordinal not in range(128)
                > I had encountered it with the askdirectory method as well. Is therean
                >easy way to bypass this?
                > Thanks again
                >
                I believe you are running into a directory or file name that has
                non-ascii characters in it. Python as shipped is set up to
                deal with ascii as its native encoding format. You can change
                this by editing the "site.py" file - look in the Lib directory
                in your python installation. Look for this code:
                >
                -------------------------------
                def setencoding():
                """Set the string encoding used by the Unicode implementation. The
                default is 'ascii', but if you're willing to experiment, you can
                change this."""
                encoding = "ascii" # Default value set by _PyUnicode_Init ()
                if 0:
                # Enable to support locale aware default string encodings.
                import locale
                loc = locale.getdefau ltlocale()
                if loc[1]:
                encoding = loc[1]
                -------------------------------
                >
                >
                >
                Change the "if 0:" to "if 1:" and see if that doesn't fix the problem.
                This is usually a bad idea, especially if the script must be distributed
                to other users in any way: since they probably never did this "trick", the
                code will fail when they use it.

                I know it's a pain, but you *have* to deal with encodings yourself, and
                not let the system guess what you might want.

                In the OP's case, the problem just lies in the 'print' statement, that
                tries to encode the file name in ASCII before printing it. So just doing:
                print repr(file.name)
                would solve the problem.

                HTH
                --
                python -c "print ''.join([chr(154 - ord(c)) for c in
                'U(17zX(%,5.zmz 5(17l8(%,5.Z*(9 3-965$l7+-'])"

                Comment

                • jmdeschamps@gmail.com

                  #9
                  Re: Problem getting a file pathname with tkFileDialog


                  Tim Daneliuk wrote:
                  Sefyroth wrote:
                  Thanks,

                  but I get this error when I try this.

                  UnicodeEncodeEr ror: 'ascii' codec can't encode character u'\xe8' in
                  position 12: ordinal not in range(128)

                  I had encountered it with the askdirectory method as well. Is there an
                  easy way to bypass this?

                  Thanks again
                  >
                  I believe you are running into a directory or file name that has
                  non-ascii characters in it. Python as shipped is set up to
                  deal with ascii as its native encoding format. You can change
                  this by editing the "site.py" file - look in the Lib directory
                  in your python installation. Look for this code:
                  >
                  -------------------------------
                  def setencoding():
                  """Set the string encoding used by the Unicode implementation. The
                  default is 'ascii', but if you're willing to experiment, you can
                  change this."""
                  encoding = "ascii" # Default value set by _PyUnicode_Init ()
                  if 0:
                  # Enable to support locale aware default string encodings.
                  import locale
                  loc = locale.getdefau ltlocale()
                  if loc[1]:
                  encoding = loc[1]
                  -------------------------------
                  >
                  >
                  >
                  Change the "if 0:" to "if 1:" and see if that doesn't fix the problem.
                  >
                  >
                  >
                  --
                  ----------------------------------------------------------------------------
                  Tim Daneliuk tundra@tundrawa re.com
                  PGP Key: http://www.tundraware.com/PGP/
                  you could also use encode("iso-8859-1") to nterpret just the printed
                  name such as:
                  print myfilename.enco de("iso-8859-1")

                  and by the way if you wanted the file NAME you could have used
                  openfilename() instead of openfile() ;-)

                  jean-marc

                  Comment

                  • Neil Cerutti

                    #10
                    Re: Problem getting a file pathname with tkFileDialog

                    On 2006-11-09, jmdeschamps@gma il.com <jmdeschamps@gm ail.comwrote:
                    you could also use encode("iso-8859-1") to nterpret just the
                    printed name such as: print myfilename.enco de("iso-8859-1")
                    >
                    and by the way if you wanted the file NAME you could have used
                    openfilename() instead of openfile() ;-)
                    The encoding of the filenames in the file system is available
                    from sys.getfilesyst emencoding(). That might turn out to be
                    useful when interpreting them.

                    --
                    Neil Cerutti
                    Strangely, in slow motion replay, the ball seemed to hang in the air
                    for even longer. --David Acfield

                    Comment

                    Working...