473,657 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Nov 8 '06 #1
9 2138
cd*******@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 tu****@tundrawa re.com
PGP Key: http://www.tundraware.com/PGP/
Nov 8 '06 #2
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:
cd*******@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 tu****@tundrawa re.com
PGP Key: http://www.tundraware.com/PGP/
Nov 8 '06 #3
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 tu****@tundrawa re.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.

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 tu****@tundrawa re.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.

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 tu****@tundrawa re.com
PGP Key: http://www.tundraware.com/PGP/

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundrawa re.com
PGP Key: http://www.tundraware.com/PGP/
Nov 9 '06 #6
On Wed, 08 Nov 2006 20:01:08 +0100, <cd*******@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+-'])"
Nov 9 '06 #7
On Wed, 08 Nov 2006 21:59:38 +0100, Tim Daneliuk <tu****@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+-'])"
Nov 9 '06 #8

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 tu****@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

Nov 9 '06 #9
On 2006-11-09, jm*********@gma il.com <jm*********@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
Nov 9 '06 #10

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

Similar topics

2
2509
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 tkFileDialog.askopenfilename() returns a tuple on Cancel instead of ''. I found that the Windows version still returns a string (''). Now I'm finding out that tkFileDialog.Directory().show() does this too. >>> d1=tkFileDialog.Directory().show() >>>...
2
4149
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 things up. After the file dialog, I can no longer use the Text widget (typing, selecting, it doesn't work anymore!) What am I doing wrong? TIA, --Irmen de Jong
2
4271
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 which has non-ascii Unicode path, as in Kanji characters, i.e. the usull "????" in place of the original UTF-8 code points. . After some time spent Google'ing. I don't find a discussion of this. Is there some option or configuration change I...
2
3348
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 progressbar and ListStore: basically i need to make pulse the progressbar while i'm copying files and write filenames copied on the ListStore on to the window.
1
1762
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) Why won't it open when I choose the menu item? (3) Python Cookbook gave me the sys.exit call, if I run it from IDLE it gives me a traceback. Should I be using something else?
3
1399
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 sense: - the current directory is the directory of the opened file - the root directory is the program directory (i.e., working directory of the program under execution) A pathname is processed by opening a file and displaying its contents. The...
18
3325
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 the image. The desired behavior is that when the pop-up is invoked, I want the underlying window to stay put. I don't have this problem when I run the code on my local computer but I do have it when I run the code on geocities.
12
1965
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 1072 as the line number. I set up my config as follows: logBaseConf = { 'level' : logging.DEBUG,
2
16564
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 close once I've selected and opened the file. It just stays there and when clicked on says not responding. I am using Vista, writing this file in Wing, and have the latest version of python My code for this interaction goes like this: import...
0
8411
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.