Problem getting a file pathname with tkFileDialog

C

cdroulers

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
 
T

Tim Daneliuk

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
 
S

Sefyroth

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
 
T

Tim Daneliuk

Sefyroth said:
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]
 
S

Sefyroth

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 said:
Sefyroth said:
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.
 
T

Tim Daneliuk

Sefyroth said:
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 said:
Sefyroth said:
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.
 
E

Eric Brunel

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
 
E

Eric Brunel

Sefyroth said:
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.

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
 
J

jmdeschamps

Tim said:
Sefyroth said:
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.

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
 
N

Neil Cerutti

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top