python - Avoiding shell=True in Popen -
i trying open .txt
file in windows. code follows:
subprocess.popen("c:\folder\file.txt", shell=true)
this works fine. default editor automatically opened , file loaded, however, have read somewhere before invoking calls through shell (cmd.exe in windows) less safe. how can same without it. setting shell=false
giving me error:
oserror: [winerror 193] %1 not valid win32 application
now, can try workaround:
subprocess.popen("notepad c:\folder\file.txt")
but works if notepad available, hence loses generality.
if using windows there (non portable) os.startfile
command take file path , open in default application filetype.
in case do:
import os os.startfile("c:\folder\file.txt")
note method won't work on linux , mac osx, you'll have have use own utilities this. (open
osx , xdg-open
on linux) , start them subprocess.
Comments
Post a Comment