0

here is simple example how to re-scan disk sda from python script

#!/usr/bin/python3

import subprocess
import os



command = "echo 1 > /sys/block/sda/device/rescan"
os.system(command)

in case we want to set the disk as variable as disk_name

#!/usr/bin/python3

import subprocess
import os

disk_name = sda
command = "echo 1 > /sys/block/disk_name/device/rescan"
os.system(command)

then what is the rights approach to pass the disk_name variable in to command ? , or maybe other better approach ?

we tried as the following but without success

command = " 'echo 1 > /sys/block/' + str(disk_name) + '/device/rescan' "
os.system(command)

1 Answer 1

1

Using your syntax (just remove the double-quotes, there is your mistake):

command = 'echo 1 > /sys/block/' + str(disk_name) + '/device/rescan'
os.system(command)

But you can use an F-string to make it cleaner:

command = F"echo 1 > /sys/block/{disk_name}/device/rescan"
os.system(command)
Sign up to request clarification or add additional context in comments.

2 Comments

what is the meaning of "F"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.