1

I have created a link 'verbs.txt' in one directory to a text file 'verbs.txt' in another directory, and I'd like to be able to open the target file via its link. The 'verbs.txt' file was created w/ BBEdit; the link to it was created using Mac's option-command-drag for creating links (aka aliases).

Here's the minimal example (test.py), where the working directory is set to the directory containing the link:

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import re

parsePattern = re.compile( '(.+);(.+)' )
verbsSpanish = []
verbsEnglish = []
factor = 1
verbFile = open( 'verbs.txt', 'r' )
for line in verbFile:
    match = parsePattern.search( line )
    if match:
        for i in range(factor):
            verbsSpanish.append( match.group( 1 ) )
            verbsEnglish.append( match.group( 2 ) )
    else:
        print( line + '\n')

print( 'Success' )
sys.exit()

It appears that the open() function works, but the code fails at the for statement with the following traceback:

practice ____ ./test.py
Traceback (most recent call last):
  File "./test.py", line 11, in <module>
    for line in verbFile:
  File "/Users/drjackbo/.pyenv/versions/3.8.5/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa4 in position 24: invalid start byte

The script runs correctly if 'verbs.txt' is a copy of, not a link to, the original file:

practice ____ ./test.py
Success
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
jayboh
  • 267
  • 3
  • 12
  • 2
    Welcome to SO! Please provide a [mre] including how you created B, and the error you got when trying to open it. You can [edit] the question. FWIW, I just tried opening a symbolic link (symlink) on Linux and confirmed it works properly with `open()`. – wjandrea Nov 28 '20 at 21:51
  • Does https://stackoverflow.com/questions/48862093/how-to-read-change-and-write-macos-file-alias-from-python help? – Karl Knechtel Dec 01 '20 at 00:31
  • Question 48862093 is about modifying the alias itself, not accessing the file the alias links to. I read through the question reasonably closely and don't see how I can apply it here. Thanks. – jayboh Dec 04 '20 at 00:44

0 Answers0