妍可以使用fstab
模塊,我用StringIO.StringIO
更換open('/etc/fstab', 'r')
:
import StringIO
from fstab import Fstab
s = content = """\
# this is a comment, followed by an empty line
# the next line is a syntax error
yo!
#UUID="2b0ab63e-fd1b-4d7a-b021-3827b4aa4c8b" /media/hda3 ext3 defaults 0 2
/dev/hda2 /boot ext3 defaults 0 2
/dev/hda1/ext3 defaults,errors=remount-ro 0 1
"""
fstab = Fstab()
#fstab.read(open('/etc/fstab', 'r'))
fstab.read(StringIO.StringIO(content))
output = []
_fs = []
for line in fstab.lines:
if line.has_filesystem():
_fs.append(line.raw)
else:
output.append(line.raw)
_fs = sorted(_fs)
output.extend(_fs)
output = '\n'.join(line for line in output)
print output
#You have just to write to '/etc/fstab'
Fstab模塊在我的系統中不存在,請問您能否提供其他解決方案/示例? –
@Max_il在你的例子中,你打開了一個本地文件。請編輯你的代碼並描述你可以做些什麼來獲取該文件。 – Zety