Skip to main content leehalls.net

Dirty Python for Quick Link Creation

As the sub-heading to this site suggests this maybe a place to find poor python! I was updating my photography galleries and in doing so creating ox-hugo clickable image links so that the main page shows the thumbnail and a click brings up the main image. Anyway its a pain to go C-c C-l for each and every file so i cobbled together a dirty and quick python script to trawl a directory and create a text file with the necesary ox-hugo links for clickable images.

The ox-hugo format needed for creating hyperlinked pictures has to resemble;

text code snippet start

[[file:/img/lifedeath/LifeDeath_002.jpg][file:/img/lifedeath/LifeDeath_002-thumb.jpg]]

text code snippet end

My files are are saved as

text code snippet start

Name                               Size Type                   Modified  Attr
LifeDeath_001.jpg                460 KB JPEG image      13/01/2019 18:31-------
LifeDeath_001-thumb.jpg         11.5 KB JPEG image      13/01/2019 18:31-------
LifeDeath_002.jpg                464 KB JPEG image      13/01/2019 18:31-------
LifeDeath_002-thumb.jpg         11.8 KB JPEG image      13/01/2019 18:31-------
..
..

text code snippet end

The following poor python does the job of creating the links

python code snippet start

import glob

#oxhugo link format
# [[file:/img/colour/C_ART_0052.jpg][file:/img/colour/C_ART_0052-thumb.jpg]]
output_file = open("link.txt","a")

one = "[[file:/img/lifedeath/"
three = "][file:/img/lifedeath/"
four = "]] "

files = []
for file in glob.glob("*.jpg"):
    files.append(file)

i = thumb = 0
main = 1
while i < len(files):
    try:
        complete = one + files[main] + three + files[thumb] + four + "\n"
    except IndexError:
        print("finished")
        break

    output_file.write(complete)
    main = main + 2
    thumb = thumb + 2

python code snippet end

The resulting output gives me exactly what i needed.