Get filename using filefialog.askfilename

C

cheirasacan

Well. It's driving me crazy. So simple....

I use:

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

to open a file with an open dialog box, OK. Made it.

How i get the name of the opened file?

i do :

print(file)

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

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

I had prove with print(file[1]) and print(file(1)) but does not work.

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

http://fossies.org/dox/Python-3.3.1/filedialog_8py_source.html#l00393 is
where i could reach!



Thanks
 
J

John Gordon

In said:
print(file)
the output is: <......name="file.doc"...mode=......encoding.......... >
How can i get the second member of 'file'?

If you're using the interpreter, you can type this command:

And it will display documentation for using objects of that type.
You can also use this command:

And it will display all the members and methods that the object provides.
 
T

Terry Jan Reedy

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.
 
C

cheirasacan

El martes, 7 de mayo de 2013 23:53:32 UTC+2, Terry Jan Reedy escribió:
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.

Yeah. This is an answer. A lot of thanks.
 
D

Dave Angel

El martes, 7 de mayo de 2013 23:53:32 UTC+2, Terry Jan Reedy escribió:

Yeah. This is an answer. A lot of thanks.

For a moment there, I thought you were being sarcastic, and ungrateful
at that. But I figured that it must have been my imagination.

Usually, it's better to teach a man to fish, rather than just handing
him one. But some would rather just have an answer, but not the
understanding of how to acquire it.

So, some unsolicited advice. And mixed in, maybe the actual answer to
your original question.

1) Lose googlegroups, or figure a way to avoid its bad habits. Your
responses here are doublespaced, which makes it hard to read, especially
when you include so much that has nothing to do with your response.
Also, triple posting leads to a number of problems, not the least of
which is the number of other responders who killfile anything from
googlegroups. See http://wiki.python.org/moin/GoogleGroupsPython.

2) Include enough of your code that people can actually figure out what
you're up to. You're asking a question about a method of an object
filedialog, but you never show how it's created.

3) Include enough of your error messages or diagnostic prints that we
can see what's going on.
output is: <......name="file.doc"...mode=......encoding.......... >

You elided the only important part of that line, the type of the object
you're asking about. Most of us would know that a file object has an
attribute of name. But instead I spent quite a while trying to find
what GUI library you were using that might be handing back something
representing a file.

4) include the environment you're running in. In your first thread, you
included a line:
Using Windows 7, Python 3.3, sfml 1.3.0 library, ...

Very helpful. But no such hints here.

5) try not to override builtin names with your own local variables. In
this case, you defined a local variable called file, which overwrites,
presumably by coincidence, its own type name.


Thanks for pointing out the "annotated source code" on fossies.org. I
wasn't aware of that location.
 
C

cheirasacan

..... Sarcastic.... what the kcuf ¿?¿?¿?

My english is SO bad?

Do you know me?

I can not understand this paranoia.... i only was giving thanks.
 
J

Joshua Landau

.... Sarcastic.... what the kcuf ¿?¿?¿?

My english is SO bad?

Do you know me?

I can not understand this paranoia.... i only was giving thanks.

Sarcasm and a lack thereof is very hard to see over the Internet. It's
probably just a misunderstanding :).
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top