Open file with its default program via python - python

I need to implement the user guide of my program and I was thinking on putting a button that opens the pdf, like if its double clicked on Windows Explorer.
But I've tried with os.popen(myfile) and open(myfile) and the interpreter open it in python, so I can print and it prints me the info of the object <_io.TextIOWrapper name='userguide.pdf' mode='r' encoding'cp1252' and what I need is to open it with its native application to avoid embed the pdf into the program.
Any way to do this?
Thanks

To open it in the default application for that file type:
subprocess.Popen([file],shell=True)
Considering that you are implementing a user guide, you may want to open it in a web browser.
import webbrowser
webbrowser.open_new(r'file://C:\path\to\file.pdf')

Related

Python: Start program in full screen mode

I know how to open a program from python, using the webbrowser module.
My question is this: how do I get it to open full screen?
At the moment I have this code:
import webbrowser
webbrowser.open("file.txt")
But it still opens Restored Down.
Please help!
Given the documentation, the webbrowser module does not have this capability at all.
This is not very surprising since you don't know what kind of device you are operating on, and it might not even have a graphical display!
What I would try is the following:
Use browser = webbrowser.get(SOME_BROWSER) to query which browsers are available.
Launch the browser using browser-specific command line arguments. You might be able to to do browser.args.append(FULLSCREEN_ARGUMENT) and then call browser.open_new(URL) instead of launching the browser manually using e.g.
the subprocess module.

Open file with Adobe Captivate from command line

I am attempting to open an XML file with Adobe Captivate in my script using os.system(). Here is my code:
os.system("open /Applications/Adobe\ Captivate\ 9/Adobe\ Captivate.app/ \"flashcards_template_changed.xml\"")
It works fine the issue is with the opening screen on Adobe Captivate. When the program is run one of those 'New Document' windows pops up and asks you whether you want to start a new document, what kind of document, etc. Similar to Microsoft Word.
I was wondering if anyone had any experience bypassing this menu so the file would open. When I open Adobe Captivate and open my XML file it opens perfectly so I know it is in the right format.
Any help would be great! Thanks!
If Adobe Captivate is registered as the default application for handling XML files, then you can leave out the application name altogether:
os.system("open flashcards_template_changed.xml")
Otherwise, specify the application with the -a flag:
os.system("open -a captivate flashcards_template_changed.xml")
I don't know for certain that "captivate" is a recognized name for Adobe Captivate; I'm just guessing, and I don't have access to a suitable Mac to find out. If that doesn't work, you could probably use something very similar to what you had in your post:
os.system("open -a '/Applications/Adobe Captivate 9/Adobe Captivate.app' flashcards_template_changed.xml")

Is there a way to open more then one program at a time in python.

I want to write a python program that with one click I can open several different files, gmail and I.E. to a particular web address at work. I know the following will open calculator but once it opens chrome nothing else will open until chrome is closed. Any suggestions.
import os
os.system('calc.exe')
os.system('chrome.exe')
os.system('file path')
os.system('Another file path')

Is there a way to check if a program is open and then if it is, switch to it, and if it isn't, open it?

I'm currently writing a program which needs to check if a program is open before deciding to switch to it or open a new one. For example, a chrome short cut should check if chrome is already open. If it isn't then it should open a new chrome window but if it is then it should just show the existing chrome window/tab. Is there a way to write a function to do this?
Thanks in advance.
To check if a program is open I would suggest a library named psutil.
The switch of the window is more complicated and depends on the OS, for example in Windows you have to create (or get) an handle for that window and then call the OS API to change it, this is simply made using win32gui, as you can see here.

suppress/redirect stderr when calling python webrowser

I have a python program that opens several urls in seperate tabs in a new browser window, however when I run the program from the command line and open the browser using
webbrowser.open_new(url)
The stderr from firefox prints to bash. Looking at the docs I can't seem to find a way to redirect or suppress them
I have resorted to using
browserInstance = subprocess.Popen(['firefox'], stdout=log, stderr=log)
Where log is a tempfile & then opening the other tabs with webbrowser.open_new.
Is there a way to do this within the webbrowser module?
What is webbrowser.get() giving you?
If you do
webbrowser.get('firefox').open(url)
then you shouldn't see any output. The webbrowser module choses to leave stderr for some browsers - in particular the text browsers, and then ones where it isn't certain. For all UnixBrowsers that have set background to True, no output should be visible.
What about sending the output to /dev/null instead of a temporary file?
I think Martin is right about Unix systems, but it looks like things are different on Windows. Is this on a Windows system?
On Windows it looks like webbrowser.py is either going to give you a webbrowser.WindowsDefault browser, which opens the url using
os.startfile(url)
or if Firefox is present it's going to give you a webbrowser.BackgroundBrowser, which starts the browser on Windows using:
p = subprocess.Popen(cmdline)
It looks like only Unix browsers have the ability to redirect stderr in the webbrowser module. You should be able to find out what browser type you're getting by doing
>>> webbrowser.get('firefox')
In a Python interactive console.

Categories

Resources