0

I am trying to run the following in a cocoa app:

cat PATHTOFILE | python -mjson.tool > OUTPUTFILE

    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/cat"];

    NSArray *arguments = [NSArray arrayWithObject: path];
    [task setArguments: arguments];

    NSPipe *pipe = [NSPipe pipe];
    [task setStandardOutput:pipe];
    [task launch];

    NSTask *task2 = [[NSTask alloc] init];
    [task2 setLaunchPath:@"/usr/bin/python"];
    NSArray *arguments2 = [NSArray arrayWithObject:[NSString stringWithFormat:@"-mjson.tool > %@.beautify", path]];
    [task2 setArguments:arguments2];
    [task2 setStandardInput:pipe];

    NSPipe *pipe2 = [NSPipe pipe];
    [task2 setStandardOutput:pipe2];
    [task2 launch];

However I am getting the following error: /usr/bin/python: Import by filename is not supported.

Any ideas?

mootymoots
  • 4,545
  • 9
  • 46
  • 74

1 Answers1

0

Seems more like a Python error.

/usr/bin/python -mjson.toolcalls the following file : (when used from terminal)

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/tool.py

whose content is

""" headers..."""
import sys
import json

def main():
    if len(sys.argv) == 1:
        infile = sys.stdin
        outfile = sys.stdout
    elif len(sys.argv) == 2:
        infile = open(sys.argv[1], 'rb')
        outfile = sys.stdout
    elif len(sys.argv) == 3:
        infile = open(sys.argv[1], 'rb')
        outfile = open(sys.argv[2], 'wb')
    else:
        raise SystemExit(sys.argv[0] + " [infile [outfile]]")
    try:
        obj = json.load(infile)
    except ValueError, e:
        raise SystemExit(e)
    json.dump(obj, outfile, sort_keys=True, indent=4)
    outfile.write('\n')


if __name__ == '__main__':
    main()

You task can't resolve these import statements. Surely because it doesn't know as much as the terminal does, and can't find this... See NSTask is not launching Python with correct path

Community
  • 1
  • 1
Vinzzz
  • 11,746
  • 5
  • 36
  • 42