473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.askopenfile.
********************************************
import tkFileDialog
file = tkFileDialog.askopenfile()
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

Nov 8 '06 #1
9 2123
cd*******@hotmail.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.askopenfile.
********************************************
import tkFileDialog
file = tkFileDialog.askopenfile()
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 tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Nov 8 '06 #2
Thanks,

but I get this error when I try this.

UnicodeEncodeError: '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:
cd*******@hotmail.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.askopenfile.
********************************************
import tkFileDialog
file = tkFileDialog.askopenfile()
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 tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Nov 8 '06 #3
Sefyroth wrote:
Thanks,

but I get this error when I try this.

UnicodeEncodeError: '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.getdefaultlocale()
if loc[1]:
encoding = loc[1]
-------------------------------

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

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Nov 8 '06 #4
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.

UnicodeEncodeError: '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.getdefaultlocale()
if loc[1]:
encoding = loc[1]
-------------------------------

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

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Nov 9 '06 #5
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.

UnicodeEncodeError: '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.getdefaultlocale()
if loc[1]:
encoding = loc[1]
-------------------------------

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

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Nov 9 '06 #6
On Wed, 08 Nov 2006 20:01:08 +0100, <cd*******@hotmail.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.askopenfile.
********************************************
import tkFileDialog
file = tkFileDialog.askopenfile()
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.askopenfilename?

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Nov 9 '06 #7
On Wed, 08 Nov 2006 21:59:38 +0100, Tim Daneliuk <tu****@tundraware.com>
wrote:
Sefyroth wrote:
>Thanks,
but I get this error when I try this.
UnicodeEncodeError: '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.getdefaultlocale()
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.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Nov 9 '06 #8

Tim Daneliuk wrote:
Sefyroth wrote:
Thanks,

but I get this error when I try this.

UnicodeEncodeError: '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.getdefaultlocale()
if loc[1]:
encoding = loc[1]
-------------------------------

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

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.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.encode("iso-8859-1")

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

jean-marc

Nov 9 '06 #9
On 2006-11-09, jm*********@gmail.com <jm*********@gmail.comwrote:
you could also use encode("iso-8859-1") to nterpret just the
printed name such as: print myfilename.encode("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.getfilesystemencoding(). 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
Nov 9 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tim Williams | last post by:
I'm using Python 2.3.2 and tkFileDialog in a program I have. I'm trying to use this on Linux (RH 8.0) and Windows 2000. When I first upgraded from v2.2 to v2.3. I noticed that...
2
by: Irmen de Jong | last post by:
Hi, I'm having trouble with the code below. It's just a regular Tk text widget in which you can type and select text as expected, however the call to tkFileDialog.askopenfilename() seems to screw...
2
by: Read Roberts | last post by:
I have the current Windows binary install of Python 2.3.4 on my Windows XP system. I am pained to discover that tkFileDialog.askdirectory() returns a mangled path when a directory is selected...
2
by: Samuele Giovanni Tonon | last post by:
hi, i'm trying to develop a trivial application which random copy files from a directory to another one. i made it using pygtk for the graphical interface, however i find some problem with...
1
by: Lith | last post by:
Here's a simple script, where I want to use a GUI to select a file. (And eventually do something interesting with it.) Some problems appear. (1) Why does it call MenuOpen automatically? (2)...
3
by: Jason Heyes | last post by:
I want to open a file with a known location and process its contents. The contents are to be treated as a list of pathnames. Each pathname is an absolute or relative pathname in the following...
18
by: len.hartley | last post by:
Hi, I am trying to pop-up a window when the user clicks on an image. The problem is that when the user clicks on the image and the window pops up OK, but the window underneath also proceeds to...
12
by: Tekkaman | last post by:
I'm getting a strange behaviour from the "pathname" and "lineno" formatter mapping keys. Instead of my file and my line number I get: /usr/lib/python2.4/logging/__init__.py as the file, and...
2
by: Traclo | last post by:
Hello all! I have a problem concerning tkFileDialog. When I use the the askopenfilename command to open a text file it opens a tkinter window (apart from the file browsing window) which refuses to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.