Getting an I/O error with a shell script to write to the file system

I write a simple Python script to save data to the file system. When I run it from the terminal, it works fine.

Script:

html = 'some text'
html_file_name = 'test.html'

html_file = open(html_file_name, "w")
html_file.write(html)
html_file.close()

However, when I try to execute it from Keyboard Maestro, I get this error:

Traceback (most recent call last):
  File "/Users/name/Desktop/save_html.py", line 12, in <module>
    html_file = open(html_file_name, "w")
IOError: [Errno 13] Permission denied: 'test.html'

Line 12 is referring to `open(html_file_name, “w”)

Woops. Wasn’t pathing correctly!

import os
script_dir = os.path.dirname(__file__)
rel_path = 'test2.html'
final_file_path = os.path.join(script_dir, rel_path)
2 Likes