Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
1  
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 at 7:28
1  
@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 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 at 7:38
add comment

2 Answers 2

up vote 7 down vote accepted

You can using fstab module.

Its documentation here.

Example:

fstab = Fstab()

for entry in fstab.entries:
    pprint.pprint(entry)
share|improve this answer
add comment

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:

 [section_name1]
 key1 = value1
 key2 = value2
 [section_name2]
 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
add comment

Your Answer

 
discard

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.