suppress/redirect stderr when calling python webrowser - python

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.

Related

Reaching internal chrome pages with Python webbrowser

I have a program where I'm using Python's webbrowser module to open a browser and navigate to a page automatically. My code essentially looks like the following:
import webbrowser
chrome_path = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
url = "stackoverflow.com"
webbrowser.get(chrome_path).open(url)
When doing it with a normal site it works exactly as expected. However, when I instead substitute in an internal Chrome site of the format chrome://<page> (e.g. chrome://dino or chrome://version) for the url, Chrome opens as expected, but it does not navigate anywhere but rather instead stays on my new tab page.
Why are normal urls (and even strings such as "hello world") working as expected, but only chrome-specific pages not? Is there any way to get around this?
(This is on Windows 10 & Python 3.6.8 by the way).
It indeed does not work, but it's not webbrowser's fault.
A small diving into the code shows that, at the end of the day, webbrowser simply calls subprocess.Popen(args) where args ending up being
'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe <url>'.
If you simply open a terminal window and execute
"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" chrome://dino
you will get the exact same behavior: Chrome opens and stays on the home page, so the problem is lying somewhere in Chrome's code (either a bug or a design choice).
It works with selenium since I assume it is using some black-OS-magic (ie interprocess communication) so it does not rely on Chrome's code. It just mimics a user.

Open file with its default program via 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')

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.

How to clear console in sublime text editor

How to clear console in sublime text editor.
I have searched on internet too..But can't find proper shortcut for that.
Please provide info
I assume you're talking about the console accessible via View -> Show Console or Ctrl`. Unlike other answers on SO, which deal with clearing the Python console when opened from the command prompt, os.system("cls") or os.system("clear") (depending on your OS) don't work with the Sublime console. The Sublime API (version 2 or version 3) does not have any built-in console-clearing method, and I was unable to find any undocumented method in sublime.py or sublime_plugin.py. The console appears to be read-only, as selecting all the text and hitting Delete doesn't work either.
I've been looking into this for some time, and I've come to the conclusion that it does not appear to be possible. However, there is a hackish workaround - just run print('\n'*100) to print 100 newline characters, and you won't be able to see any of the previous output unless you scroll up some distance.
I installed ClearConsole package, then type alt+k to clear then console.
A new window will always have a clean console. You can use this to open a new window, close the old one and then reopen your project (assuming it is a saved project and not anonymous). This requires the hot_exit setting to be true, which is the default.

Python webbrowser outputting page to console on Windows

I'm writing a PyGTK application on Windows, and in a function, I'm trying to open a webpage with the webbrowser module. It should be the simplest thing in the world, but instead of opening in a browser, it prints the HTML source of the page to the console. Does anyone know why this would happen?
The code in question:
oauthURL = ("http://api.twitter.com/oauth/authorize?oauth_token=" + requestToken)
webbrowser.open(oauthURL, 2, True)
I tested it on my Arch Linux laptop just now, and it works fine, so this is a Windows-specific problem. Perhaps Python can't find a browser to use?
What it normally tries to do (on Windows) is using os.startfile(url), which launches the default application for http urls. You can check what this does by typing start http://www.example.com at the command prompt. If this has the same effect, you know what the problem is, and you should reconfigure your default browser in Windows.
This behaviour can be overridden by the user, through the BROWSER environment variable, so you might want to check that if the above doesn't help.
This code snippet can handle opening a web-page in a cross-platform way:
if sys.platform[:3] == 'win':
try:
os.startfile(oauthURL)
except WindowsError as why:
# your error handling code
print(why)
else:
webbrowser.open(oauthURL, 2, True)
It is based on IDLE's EditorWindow.py "python_docs" function.

Categories

Resources