Reputation: 1886
I want to retrieve the URL property of a Windows internet shortcut (.url) file. For example, there is a YouTube trailer "ROGUE ONE: A Star Wars Story TRAILER (2016)" at https://www.youtube.com/watch?v=Ze2kpOZx_kU. If the URL is dragged from a Chrome browser to the Windows desktop a file "ROGUE ONE- A Star Wars Story TRAILER (2016) - YouTube.url" is created. In Windows 10 I can look at the file's property (e.g. right click and select 'Property'), select the 'Web Document' tab, and in the URL field there is "https://www.youtube.com/watch?v=Ze2kpOZx_kU". How do I get this URL programmtically in Python 2.7?
Upvotes: 2
Views: 2642
Reputation: 346
I've extracted all url strings from multiples .url
files as below Python 3.9.5 script:
import glob
import os
for filename in glob.glob('*.url'):
with open(filename, "r") as infile:
for line in infile:
if (line.startswith('URL')):
url = line[4:]
print (url)
break
Note: save the above script as .py
file inside same folder that contain .url
files.
Upvotes: 0
Reputation: 5349
Windows Internet Shortcut (.url
) files are INI files. They may have one or more sections (for apps or frames) and several properties (like IconFile
or HotKey
) but they always have an InternetShortcut
section with a URL
property.
So you can parse them with ConfigParser
:
import ConfigParser
parser = ConfigParser.ConfigParser()
file = 'C:\Temp\ROGUE ONE- A Star Wars Story TRAILER (2016) - YouTube.url'
parser.read(file)
url = parser.get('InternetShortcut', 'url', True)
Arguments in get
are:
raw
: True
disables interpolation. Needed to avoid mistaking url's percent-encoding for inner references.Upvotes: 2
Reputation: 1886
I was chasing this problem the wrong way! I thought I needed to extract the file information. Using Philip's suggestion I printed the contents and saw that the URL was inside. For example, dragging the movie's URL to C:\Temp and running the following I got the URL:
filename = 'C:\Temp\ROGUE ONE- A Star Wars Story TRAILER (2016) - YouTube.url'
with open(filename, "r") as infile:
for line in infile:
if (line.startswith('URL')):
url = line[4:]
break
print url
This gives: https://www.youtube.com/watch?v=Ze2kpOZx_kU
Upvotes: 5