469,288 Members | 2,320 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,288 developers. It's quick & easy.

check disc space

Any ideas how to check for free disc space from Python. Platform
independent?
Thanks for any suggestion.
Thomas

Jul 18 '05 #1
6 4690
Thomas Schulz <th****@esands.com> wrote...
Any ideas how to check for free disc space from Python. Platform
independent?


Fortunately for you, my bad stomach will not allow me but a precious
hour of sleep this fine morning. :)

http://botanicus.net/dw/tmp/DiskFree.py

Tested: Win32, Linux, FreeBSD. I'm going to work in about 3 hours.
This _really_ sucks. :(
David.
Jul 18 '05 #2
Thanks for that.
I wonder if it would be worthwhile to have such a functionality
somewhere in the os module?

Thomas
David M. Wilson wrote:
Thomas Schulz <th****@esands.com> wrote...

Any ideas how to check for free disc space from Python. Platform
independent?

Fortunately for you, my bad stomach will not allow me but a precious
hour of sleep this fine morning. :)

http://botanicus.net/dw/tmp/DiskFree.py

Tested: Win32, Linux, FreeBSD. I'm going to work in about 3 hours.
This _really_ sucks. :(
David.


Jul 18 '05 #3
"T. Schulz" wrote:
I wonder if it would be worthwhile to have such a functionality
somewhere in the os module?


two counter-arguments:

- the os module is traditionally used to provide access to posix-style
services provided by the OS (or the C library). in this case, os.statvfs
is such a function; calling out to "du" isn't

(shouldn't that be "df", btw? os.popen("du -k dir").split()[-5] sure
doesn't give the free diskspace on any platform I have access to...)

- what you mean with "free disc space" may depend on the platform
and the application; os.statvfs provides lots of additional information
that you can use on a Unix platform, and there are similar (but not
identical) metrics available on Windows.

(a third argument is that "free disc space" is a rather meaningless metric
on a modern operating system; it's hardly ever useful for anything except
"warning, warning, less than 5% free space" alerts. you really don't want
people to write "if os.getfreespace() >= len(buffer): f.write(buffer)" and
file a bug when write fails ;-)

</F>


Jul 18 '05 #4
"Fredrik Lundh" <fr*****@pythonware.com> wrote...
- the os module is traditionally used to provide access to posix-style
services provided by the OS (or the C library). in this case, os.statvfs
is such a function; calling out to "du" isn't (shouldn't that be "df", btw? os.popen("du -k dir").split()[-5] sure
doesn't give the free diskspace on any platform I have access to...)
Um, yep. Fixed. *sheepish shrug*

(a third argument is that "free disc space" is a rather meaningless metric
on a modern operating system; it's hardly ever useful for anything except
"warning, warning, less than 5% free space" alerts.
Considering what I see python used for on a day-to-day basis, this
isn't "hardly ever useful". I have seen jobs fail as a direct result
of no disk space check. This should indeed be handled elsewhere and in
a different fashion, but sometimes the world just ain't the ideal
place you'd like to believe it is.

When you are performing 4gb deduplication runs on a cluster, and have
to wait a couple of days only to find out you don't have enough disk
space to write out the last 5 records of the result tables, I think
this silly little module can be justified.

And why not just use the POSIXy interface provided by os? Well, the
company I work for uses the same codebase for both Windows and Linux
clustering, which is a quite a feat. All support tools as such must
run under both operating systems too.

you really don't want
people to write "if os.getfreespace() >= len(buffer): f.write(buffer)" and
file a bug when write fails ;-)


Certainly not! :)

But "if estimated_space > int(free_space * 1.05): ..." might not be
such a bad thing when there is no other route to sanity available.

Thanks,
David.
Jul 18 '05 #5
Fredrik Lundh wrote:
(a third argument is that "free disc space" is a rather meaningless metric
on a modern operating system; it's hardly ever useful for anything except
"warning, warning, less than 5% free space" alerts. you really don't want
people to write "if os.getfreespace() >= len(buffer): f.write(buffer)" and
file a bug when write fails ;-)


Well, I think that for installation programs, downloaders and file
managers, for example, it would be useful.

yours,
Gerrit.

--
122. If any one give another silver, gold, or anything else to keep, he
shall show everything to some witness, draw up a contract, and then hand
it over for safe keeping.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

Jul 18 '05 #6

But "if estimated_space > int(free_space * 1.05): ..." might not be
such a bad thing when there is no other route to sanity available.


I support that.

In my current project, I know that I get 1GB of data during the next 15
minutes or so. I would like to make sure that there is enough space
available.

We develop on Linux but support Windows as well.
Certainly we get there if we use os.statvfs here and
win32api.GetDiskFree there. But I belive the question is quite common
and why shouldn't there be a os independent abstraction? Specially since
almost all current hardware uses some kind of file system to store things.

Thomas

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Alex Polite | last post: by
7 posts views Thread by YT | last post: by
9 posts views Thread by DataSmash | last post: by
1 post views Thread by =?Utf-8?B?T2FrZGVuZQ==?= | last post: by
1 post views Thread by =?Utf-8?B?amlt?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.