I need to programmatically be able to see how much space is left on the SD card and if it is low delete some files. I'm not sure how to go about getting the value of the SD card space and then saving that as a variable to use in the if statement. I've read that df shows this info but how would I save that as a variable or get that info another way?


available_space = #the available space on the sd card
If( available_space < 1gb):
      #delete old files 
  • 5
    This isn't specific to Raspberry Pi. Do man df. You can call that from whatever program you're writing.
    – Brick
    Jun 20, 2019 at 14:59
  • I think it would be valuable to Raspberry Pi users to have a place to learn more about freeing up space, and warning against automating it completely. Going elsewhere and asking this question, details such as the fact that "SD Card" is the main filesystem, vs a throwaway disk, could cause confusion for people who want to learn 'how to safely use their Raspberry Pi', vs 'how to administer a Linux System'.
    – earthmeLon
    Jun 20, 2019 at 19:54
  • @earthmeLon I think you misread the question. This is a question of how to call df from Python and parse the result. The SD Card has nothing to do with it nor does the wisdom of (or lack thereof) automating deletion. The OP has told us nothing about which files will be freed nor how / if it's know that those files are ok to go.
    – Brick
    Jun 20, 2019 at 20:08
  • Are you really wanting/requiring to solve this problem in Python, or are you simply looking to solve your problem, potentially in Python? I got confused when you brought up df because df is available in BASH, which can also be used programatically.
    – earthmeLon
    Jun 20, 2019 at 20:12

2 Answers 2


To get the available disk space with python you could use psutil

import psutil

path = '/'
bytes_avail = psutil.disk_usage(path).free
gigabytes_avail = bytes_avail / 1024 / 1024 / 1024

If you look at the source code of the disk_usage function, you'll see they use os.statvfs().

To use statvfs yourself you could do something like this

import os

path = '/'
st = os.statvfs(path)

# free blocks available * fragment size
bytes_avail = (st.f_bavail * st.f_frsize)
gigabytes = bytes_avail / 1024 / 1024 / 1024

Unless you provide a safe limitation (ie: "Only files in /mnt/volatile"), it's not a good idea to completely automate the removal of files. You can look into tmpwatch/tmpreaper on how Linux and different distributions create temporary files and directories, to hook into, or take advantage of the existing setup.

For example, if you can put unimportant files into /tmp/, they will be removed at reboot, and automatically by the system, over time, if space is needed.


Depending on your distribution and setup, you can try using baobab aka Disk Usage Analyzer.

baobab allows you to look into where data is located, either by disk, partition or directory. The visual nature makes it easier to investigate and find removable data than CLI solutions.


If you're interested in using the CLI, you can take a few approaches:

  1. Use find to find files larger than a certain size.
  2. Use du to look at files/directories.
    • -c for total
    • -d for depth
    • path/to/inspect/* Trailing * for directories
    • | sort -n Put largest files/directories at bottom
    • | tail -n 100 to limit the results

To look at a path and find the '20 'largest directories:

du /path/to/inspect/* | sort -nr | tail -n 20

You can drill down into directories you find:

$ du /path/to/inspect/* | sort -nr | tail -n 20
$ du /path/to/inspect/LargestDirectoryFound/* | sort -n | tail -n 20

Once you determine how you like to find the files/directories, you will want to turn it into a bash function for easier use:

$ # Put this function in .bashrc after testing and tuning
$ function du-search(){
    # $@ is everything after the command
    du $@* | sort -n | tail -n 20
$ du-search /var/
$ du-search /var/INTERESTING_PATH/

By looking into du and the available options, you can show fewer duplicates and 'navigate' more quickly:

$ function du-search(){
    # $@ is everything after the command
    du -S -d0 $@* | sort -n | tail -n 20
$ du-search /var/
$ du-search /var/log/
 1820814528 /var/log/some_huge_logfile.log
  • Please help me to understand why down-voting this answer was necessary. Please describe how it could be improved to help answer the Question.
    – earthmeLon
    Jun 20, 2019 at 19:56
  • 2
    If I were to vote on it, I would vote it down because 1. off-topic questions should not be answered, and 2. it doesn't answer the question anyway. On the latter, the OP wants to know how to call df from Python and parse the result, which you've not addressed.
    – Brick
    Jun 20, 2019 at 20:06
  • Oh the python tag. Missed that. I don't agree with your 1. as a reason to downvote an Answer, but I see how this is not valid if a Python solution is required. Thanks for helping clarify.
    – earthmeLon
    Jun 20, 2019 at 20:17
  • 1
    Downvoting answers on off-topic questions facilitates the questions being deleted by the system, so it seems a defensible option for someone who felt the question was particularly off-topic. (meta.stackexchange.com/questions/5221/…) That is the norm on several SE sites. But in this case, the Python aspect seems more important. Could be a third reason though...
    – Brick
    Jun 20, 2019 at 20:51
  • I'm still very curious as to how important Python is to the Answer. You've raised a very good point and I'm waiting to see if the Question will allow the use other programmatic approaches and myself question the weight/validity of the included Python tag. I'll definitely remove this if Python is a requirement and there's no X Y.
    – earthmeLon
    Jun 20, 2019 at 22:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.