Search code examples
pythoncommand-lineexecute

Opening file from python


In my python file, I create an *.bmp image, which I the open to check everything went fine.

It is a bit of a hassle to open this each time I run my program (I know, I'm too lazy) and using show seems counterintuitive, since it freezes the execution and I can't keep it open.

Which is the command in python to open files?, you know like a:

command='gedit file.txt'
pythonmagic.execute(command)

Obviously pythonmagic and the syntax are the things I'm missing.

Thanks a lot


Solution

  • Perhaps you want the subprocess module.

    import subprocess
    proc = subprocess.Popen(['gedit', 'file.txt'])
    

    The above code will open gedit with the single argument 'file.txt' (i.e. open the file in gedit).

    If you add the line,

    proc.wait()
    

    then the script will wait until you close gedit, should you ever want that.