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

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -