file/directory format/size help

M

moishyyehuda

Hi

I am writing a script to upload images. How do I check the format of
the file? How can I tell if the file is an image, movie, or text file?
I would also like to put a limit on how much a user can upload. So how
can I check how many bits, bytes, mb, gb are in a folder.

So that sums up to

#1 What type of file is the file? Is it a movie, image, or text
document?
#2 What particular form is the file jpeg, bmp, gif etc.?
#3 The size of a directory?

Thanks
 
S

Steven D'Aprano

Hi

I am writing a script to upload images. How do I check the format of
the file? How can I tell if the file is an image, movie, or text file?
I would also like to put a limit on how much a user can upload. So how
can I check how many bits, bytes, mb, gb are in a folder.

So that sums up to

#1 What type of file is the file? Is it a movie, image, or text
document?

In the Windows world, one simply looks at the file extension (e.g. .gif,
..avi, .txt, etc.) and hopes that it is correct.

In the Linux world, there is a command "file" that tries to guess the file
type, by looking at some combination of extension and/or signature bytes
inside the file.

Classic Macintosh OS kept file-type metadata as part of the file system; I
don't know what OS X based Macintoshes do.

Why do you care what the format of the file is? Can't you let the user
upload whatever file they like?

#2 What particular form is the file jpeg, bmp, gif etc.?

I don't understand the question. What do you mean "form"?

#3 The size of a directory?

Directories themselves generally are a fixed size, e.g. on my ext3 file
system my directories are typically 4K in size. But that's probably not
what you mean :)

I assume you mean the size of all the files in a directory combined. You
have to get the size of each file, and add them all together. If you use
Google, you will find Python code to do that. Try looking on the
ActiveState Python Cookbook:

http://aspn.activestate.com/ASPN/Python/Cookbook/
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top