file/directory format/size help
Steven D'Aprano
steve at REMOVE.THIS.cybersource.com.au
Sun Dec 24 16:58:51 EST 2006
On Sun, 24 Dec 2006 07:45:32 -0800, moishyyehuda wrote:
> 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/
--
Steven.
More information about the Python-list
mailing list