I have a linux proc entry in
echo 1 > /proc/sys/fs/offs/ts/enable
echo 0 > /proc/sys/fs/offs/ts/enable
with open('/proc/sys/fs/offs/ts/enable', 'w') as p:
There are a couple of problems with your code.
Firstly, you want to write to the file, but you're opening it in read mode.
.write expects string data, not an integer.
We can get rid of the
if test by exploiting the fact that
True have integer values of 0 & 1, respectively. The code below uses the
int(enable) to a string. Also,
end argument), so this way the Python code performs the same action as your Bash command lines.
def set_mode(enable=True): with open('/proc/sys/fs/offs/ts/enable', 'w') as p: print(int(enable), file=p)
If you want to do it with
.write, change the
p.write(str(int(enable)) + '\n')
There's a way to do that conversion from boolean to string in one step: use the boolean to index into a string literal:
It's short & fast, but some would argue that it's a little cryptic to use booleans as indices.