0

Hi,
I was hoping to create an application where the user can open a file from their hard drive (using the askopenfile dialogue from tkFileDialog) and it would display information such as file name / size.

I managed to get everything working correctly except for displaying the information. I tried to copy it to a temporary file and then read the size of the temporary file but this caused "TypeError: must be string or buffer, not file" for which I converted it into a string but it still returns the incorrect value (I am testing it on a file of my own that I know the size of). I am using the os module to display the size of the temporary file after it has had the contents from the askopenfile variable copied into it.

I basically want to know the size of the file using the os.path.getsize function but on the file that is already open

It has been wracking my brain and I am completely perplexed by this.

4
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by slate
0

I think there is a method askopenfilname() which returns only a file name instead of an open file. Otherwise, file objects have an attribute 'name' which you could use.

0

You can use os.path.getsize on the file if it is open. But you have to use it on the filename not the file object.

f=open("vmi","w")
f.write("asdf")
import os
os.path.getsize("vmi")
#0 byte
f.close()
os.path.getsize("vmi")
# 4 bytes
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.