Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to automatically configure my fstab in python by running a script. I thought of using ConfigParser in python, but I am unable to use it properly.

share|improve this question
I voted to migrate this to StackOverflow. You may be speaking about editing a particular file on a *nix OS, but your asking about doing so in a programming language (which means, to me, that the answer is more concerned with programming than with *nix). – HalosGhost Jul 16 '14 at 7:28
@HalosGhost shell programming is often about editing a particular file and it is always in some programming language, as long as it is for U&L administrative purposes, I don't think we should move all of those questions. – Anthon Jul 16 '14 at 7:37
@Anthon, shells are often *nix specific. This isn't a shell programming question, it's a Python question. But, I don't have the ability to migrate; it was just a vote. – HalosGhost Jul 16 '14 at 7:38
up vote 8 down vote accepted

You can using fstab module.

Its documentation here.


fstab = Fstab()

for entry in fstab.entries:
share|improve this answer

This will not work because /etc/fstab file doesn't conform to the format the ConfigParser supports.

As man 5 fstab indicates, the /etc/fstab file is a whitespace separated file with 6 columns. The Python ConfigParser is a library for Parsing files commonly known as .ini files that, simplified, consist of sections that contain key/value pairs:

 key1 = value1
 key2 = value2
 key3 = value3

Parsing the fstab file yourself is relatively easy. Just copy all the comment lines (those with first non-whitespace character '#') and split the other lines using .split() to the 6 fields. I use that to rearrange my fstab file to be only 79 columns.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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