title: Python code to check free space of a USB drive connected to the Linux machine
body: <p>I want to know, is there any way to get free/available space of a usb drive when I connect it to a Linux machine. I extracted following code from internet. It is working for root drive. but I have no idea to set the path dynamically.</p>
  
  <pre><code>import os
  
  disk = os.statvfs("/")
  
  print""
  print "~~~~~~~~~~calculation of disk usage:~~~~~~~~~~"
  print""
  
  totalBytes = float(disk.f_bsize*disk.f_blocks)
  print "total space: %.2f GB" % (totalBytes/1024/1024/1024)
  
  totalUsedSpace = float(disk.f_bsize*(disk.f_blocks-disk.f_bfree))
  print "used space: %.2f GB" % (totalUsedSpace/1024/1024/1024)
  
  totalAvailSpace = float(disk.f_bsize*disk.f_bfree)
  print "available space: %.2f GB" % (totalAvailSpace/1024/1024/1024)
  </code></pre>
  
  <p>Help me to develop that code or any idea for another code to get details about free or available spaces of the usb drive I connected.</p>