So, I’m making a cd burning app and I need to eject the drive to let the user put the disk in. It’s a little more complicated, but simplest case I run into is this; I can use cdrecord via the command line to eject the cd tray using this command:
cdrecord --eject dev='/dev/sg1'
which should mean that I can do the same thing with subprocess.call, like this:
subprocess.call(["cdrecord", "--eject", "dev='/dev/sg1'"])
however, when I do that, I get this error:
wodim: No such file or directory.
Cannot open SCSI driver!
For possible targets try 'wodim --devices' or 'wodim -scanbus'.
For possible transport specifiers try 'wodim dev=help'.
For IDE/ATAPI devices configuration, see the file README.ATAPI.setup from
the wodim documentation.
and the tray doesn’t open.
This is a very similar error to one I got before when trying to run it form the command line, but I fixed that error by loading the sg kernel module.
If I just run:
subprocess.call(["cdrecord", "--eject"])
it opens the tray just fine. However, this needs to work with possibly multiple cd trays, so that won’t work.
How can I get this to eject the cd correctly?
Try this:
The shell will take care of interpreting the quotes, but
cdrecordwill not.The only reason you need the quotes in the first place is that the
devpath might have spaces in it, causing the shell to split things into separate arguments. For example, if you type this:The arguments to
cdrecordwill be--eject,dev=/dev/my,silly,cd,name. But if you do this:The arguments to
cdrecordwill be--eject,dev=/dev/my silly cd name.When you’re using
subprocess.call, there’s no shell to pull the arguments apart; you’re passing them explicitly. So, if you do this:The arguments to
cdrecordwill be--eject,dev=/dev/my silly cd name.In some cases—e.g., because you get things in a hopelessly confused state in the first place (e.g., you’re reading a config file that’s meant to be used by your program or executed by the shell)—you really have no recourse but to run through the shell. If that happens, do this:
But this generally isn’t what you want, and it isn’t what you want in this case.