0

Is there an equivalent to the "os.listdir()" method using the PyObjC library? I want to list all of the files in a directory.

I am making my python script into an executable using PyInstaller. There is a known issue where some of the "os" functionality cannot be used when launching the app in MacOS.

I found this method in the Foundation framework which seems like it would work, but I have not been able to convert it into the right PyObjC syntax (see error below).

NSFileManager.defaultManager().contentsOfDirectoryAtPath_('/Users/eturner/Desktop', true)
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    NSFileManager.defaultManager().contentsOfDirectoryAtPath_('/Users/eturner/Desktop', true)
AttributeError: 'NSFileManager' object has no attribute 'contentsOfDirectoryAtPath_'
Trey
  • 201
  • 3
  • 14

1 Answers1

0

The name of the objetive-c selector for this method is contentsOfDirectoryAtPath:error:. This means that the Python method is named contentsOfDirectoryAtPath_error_ and should be used as such:

contents, error = NSFileManager.defaultManager().contentsOfDirectoryAtPath_('/Users/eturner/Desktop', None)

The "error" argument for the Objective-C selector is a pass-by-reference output argument, that's why None is passed for the value and the return value of the Python call contains 2 values: the actual return value and the value of the pass-by-reference output argument.

Ronald Oussoren
  • 2,715
  • 20
  • 29