最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

process - Swift code to convert .py files to .mpy using mpy-cross - Stack Overflow

programmeradmin2浏览0评论

So I thought I'd have a bit of fun writing a small app in Swift to batch convert .py files to .mpy, using mpy-cross. mpy-cross works fine from the command line - it takes a .py file and outputs a new .mpy file in the same location - but I get an error when I call it using Process in Swift. Here's the code -

    func convert(filePath: String) -> Bool {
        
        let task = Process()
        let pipeOut = Pipe()
        let command = "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/mpy_cross/mpy-cross " + filePath
        
        task.standardInput = nil
        task.standardOutput = pipeOut
        task.standardError = pipeOut
        task.arguments = ["-c", command]
        task.executableURL = URL(fileURLWithPath: "/bin/zsh")
        
        do {
            try task.run()
            
            let data = pipeOut.fileHandleForReading.readDataToEndOfFile()
            let output = String(data: data, encoding: .utf8)!
            
            print(output)
            
        } catch {
            return false
        }
        
        return true
    }

The error I get is OSError: (1, "can't open {path to myfile.mpy}") and the .mpy file isn't created, but the task seems to have run ok. Any suggestions much appreciated!

So I thought I'd have a bit of fun writing a small app in Swift to batch convert .py files to .mpy, using mpy-cross. mpy-cross works fine from the command line - it takes a .py file and outputs a new .mpy file in the same location - but I get an error when I call it using Process in Swift. Here's the code -

    func convert(filePath: String) -> Bool {
        
        let task = Process()
        let pipeOut = Pipe()
        let command = "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/mpy_cross/mpy-cross " + filePath
        
        task.standardInput = nil
        task.standardOutput = pipeOut
        task.standardError = pipeOut
        task.arguments = ["-c", command]
        task.executableURL = URL(fileURLWithPath: "/bin/zsh")
        
        do {
            try task.run()
            
            let data = pipeOut.fileHandleForReading.readDataToEndOfFile()
            let output = String(data: data, encoding: .utf8)!
            
            print(output)
            
        } catch {
            return false
        }
        
        return true
    }

The error I get is OSError: (1, "can't open {path to myfile.mpy}") and the .mpy file isn't created, but the task seems to have run ok. Any suggestions much appreciated!

Share Improve this question asked Mar 17 at 23:03 SomaManSomaMan 4,1641 gold badge35 silver badges45 bronze badges 1
  • 1 Is your app in a sandbox? Which project template did you use to create the project? – Sweeper Commented Mar 18 at 7:03
Add a comment  | 

1 Answer 1

Reset to default 0

Turns out it was really simple - just disable the App Sandbox in the .entitlements - you live & learn

发布评论

评论列表(0)

  1. 暂无评论