Python3 を使用して Windows ベースのネットワーク プリンタを ubuntu にインストールすることができました。
コーディングを改善するために、ダウンロード後にドライバーを含むファイルが存在するかどうかを最初に確認したいと思います。 os.path.isfile などで可能であることはわかっていますが、os は将来サポートされなくなりますが、サブプロセスでそれを実行したいと考えています。
では、どうすればよいでしょうか? subprocess.call などを使用しますか?
------------------------
ファイルが存在するかどうかを確認するには、ファイルシステムと対話するための Python 的でポータブルな方法である pathlib を使用するのが理想的です。
ただし、チェックの時間を避けるために、使用時間エラー (TOCTTOU) 以下を考慮する必要があります。
代わりに:
if check_printer_present():
# now, after checking the printer is present,
# the printer might go offline
use_printer()
より良い使い方:
try:
use_printer()
except PrinterError():
printer_error_cleanup()
参照:
https://de.wikipedia.org/wiki/Time-of-Check-to-Time-of-Use-問題
このイディオムは次のように覚えているかもしれません:
it's better to ask forgiveness than permission
(t is better to act decisively and apologize for it later
than to seek approval to act and risk delay, objections, etc.)