Monday, June 3, 2019

A useful little Python secret ?

A Useful Little Python Secret?

A useful little Python secret-logo

Subtitled: Well I hope it is, or I am going to look a right idiot… again!

This Is All TIM’s Fault

Whilst updating TIM for my last post, and trying to make it cross platform on Windows and Linux, I came across several problems that I am now looking at closer to try resolve properly, rather than the stop-gap solutions I used just to finish off the update.

One of those problems was that TIM on Windows used various built in system utilities, like notepad.exe, which couldn’t be used on Linux.

I had trouble replicating this using Linux equivalent apps, mostly because I am a new Linux user and still unfamiliar with what apps to use.

I couldn’t get subprocess to work for just data files like a .txt file, as it appears it needs to be told what app to execute to display the data.

Text With A Scrollbar, Full Stop

I would eventually (probably) work out what apps to use on Linux, but how could I be sure all distro’s had that app? It would be far simpler if we had the option to use the current systems associated app for each data file wouldn’t it?

To get around viewing text files from inside TIM I ended up writing a little text viewer of my own as a temporary workaround. This worked okay, though it is pretty basic and unimpressive to use. It displays text with a scrollbar, full stop.

When it came to playing mp3s from within TIM, I considered experimenting with various modules, commands and utils. Eventually I wondered if a browser could play mp3s , so naturally I imported the built in webbrowser module to try it.

I wasn’t hopeful as the webbrowser docs say it handles URL’s and that’s about it. But I recall once using it to display a jpg image, so what the hell, let’s give it a try.

import webbrowser
webbrowser.open("test.mp3")

Bloody, bollocking, blinking, blimey, bro, it bally well worked.

The Webbrowser module played the mp3, not in the systems’ default webbrowser as you would expect, but using the systems’ associated app. Awesome.

For example, on my Windows 7 I still use WinAmp as my music player, and so Webbrowser used that to play the mp3, no browser was opened, this was very encouraging.

Double Awesome?

So, I shut down Windows and booted up my Linux Mint drive and tried it on there. Yup, it works on Linux too, it used x-player to play it. Double awesome..

I went on to try various other files and they all worked:

import webbrowser
#on linux<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>
webbrowser.open("test1.jpg")#plays in x-viewer, or associated app.
webbrowser.open("test.ico") #plays in x-viewer, or associated app.
webbrowser.open("test.csv") # loads into associated app
webbrowser.open("test.mp3") #plays in x-viewer, or associated app.
webbrowser.open("test.txt") #plays in xed, or associated app.
webbrowser.open("test.py")  #displays code in xed, or associated app.
webbrowser.open("test.pdf") #plays in x-reader, or associated app.
webbrowser.open("test.mp4") #plays in x-player, or associated app.

This was super for me because it meant I didn’t even need to know what apps to call, I just let webbrowser do all that work. It also meant that as long as the system in use had the file format in question associated with an app, it would work.

Webbrowser even executes .exe files on Windows. I’ve not tested that on Linux yet, but it most likely works.

A One Line Solution

What this meant for me was I now had a way of playing mp3s and displaying text from within my programs with one very simple line that would work on any Windows or Linux system, the perfect solution for the problem that I had.

I do not know if this would work on MAC, I expect it would though.

I assumed webbrowser was using subprocess to do this, and I would like to know how it can find out what the associated apps are for each file, or what syntax it used, just out of interest really. I am happy to go on using webbrowser to do this and not worry about the, (probably), complicated command line stuff.

Source Code

I looked over the source code of the webbrowser module anyway. It does import subprocess and Popen’s web browsers, but I can’t see how it opens these files via association, though there is a lot of command line type stuff going on that is way out of my league at the moment.

I then did a google for tutorials on using webbrowser ,and not one of the first 20 or so that I scanned through mentioned that the module could do this. The webbrowser docs do not mention this either, which is a bit odd, isn’t it? I also could not find a YouTube video that mentioned it either.

Undocumented Feature?

So, have I discovered an undocumented feature? Or does everyone else except me on the planet already know about this, and it is no big deal?

I suppose it doesn’t really matter.  I have solved one of my main problems of making my apps cross platform, now you have that option too.

Now I’m off to actually put this into practice.

Be seeing you.
Steve.

PS. sorry about the click bait title, but I couldn’t resist it. Also, the young lady in the logo is from a royalty free pixabay image. Thank you kindly madam.


Using Python V3.6.5 32bit, on Windows 7 64bit

and Python V3.67 on Linux Mint 19.1 Cinnamon 64bit

Home Page

Previous Post: The Insult Machine Update V1.52



Advertisements


from Python Coder
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...