Get filename using filefialog.askfilename

Terry Jan Reedy tjreedy at udel.edu
Tue May 7 17:53:32 EDT 2013


On 5/7/2013 4:27 PM, cheirasacan at gmail.com wrote:

> file = filedialog.askopenfile ( mode....... )

askopenfile is a convenience function that creates an Open dialog 
object, shows it, gets the name returned by the dialog, opens the file 
with that name, and returns an appropriate normal file object

> to open a file with an open dialog box, OK. Made it.
>
> How i get the name of the opened file?

file.name, (at least in 3.3), which in your example below is "file.doc"

> print(file)
>
> the output is: <......name="file.doc"...mode=......encoding..........  >

This is the standard string representation of a file object. It is 
created from the various attributes of the file instance, including 
file.name.

> How can i get the second member of 'file'?

Strings do not have fields. The second 'member', would be the second 
character, file[1], which is not what you want.

> And i am unable to find a detailed reference to this object in the i.net

Use the Fine Manual. The entry for builtin open() function, which you 
should read to understand the 'open' part of askopenfile, directs you to 
the Glossary entry 'file object' which says "There are actually three 
categories of file objects: raw binary files, buffered binary files and 
text files. Their interfaces are defined in the io module. The canonical 
way to create a file object is by using the open() function." The kind 
of file object you get is determined by the mode ('b' present or not), 
buffer arg, and maybe something else. You can look in the io chapter or 
use dir() and help() as John G. suggested.

Python programmers should really learn to use dir(), help(), and the 
manuls, including the index and module index.

--
Terry Jan Reedy





More information about the Python-list mailing list