Skip to main content
 首页 » 编程设计

python之从自动化器运行 python 脚本

2025年05月04日12exmyth

我正在尝试从 Automator 创建一个“.app”来运行一个简单的 python 脚本。当我在 Automator 中执行脚本时,会或多或少地提示“检查您的操作属性并再次执行”错误。

在历史记录中它说“追溯(最近一次调用:)”。关键是这个脚本在终端 session 中运行良好。

这似乎至少是我用于重命名数据库的循环“While”的错误(见下文),因为我可以执行脚本到这个阶段。
管理 sqlite 数据库有问题吗?但是我无法理解,因为终端没有问题。有什么遗漏吗?

我的 python 脚本:

#!/usr/bin/python 
 
import sqlite3 
import os.path 
 
file_name = "newDB.data" 
choice = "" 
 
if os.path.isfile(file_name): 
    choice = raw_input("Erase DB? press [y] or [n]:\n") 
 
if choice == "y": 
    print "erase" 
 
    while True:  
        try: 
            os.remove(file_name) 
            break 
 
    except OSError as e: # name the Exception `e` 
        print "Failed with:", e.strerror # look what it says 
        print "Error code:", e.code  
 
if choice == "n": 
    print "Bye!" 
    exit()  
 
 
# start sqlite connection 
conn = sqlite3.connect("newDB.data") 
c = conn.cursor() 
 
 
# attach 
c.execute("ATTACH database 'store1.data' AS db1") 
c.execute("ATTACH database 'store2.data' AS db2") 
 
 
# rename tables 
while True: 
    try: 
        c.execute("ALTER TABLE db1.ZPATIENT RENAME TO table1") 
        print "table 1 renamed" 
        break 
 
    except: 
        c.execute("ALTER TABLE db1.table1 RENAME TO ZPATIENT") 
        print "except 1" 
 
 
while True: 
    try: 
        c.execute("ALTER TABLE db2.ZPATIENT RENAME TO table2") 
        print "table 2 renamed" 
        break 
 
    except: 
        c.execute("ALTER TABLE db2.table2 RENAME TO ZPATIENT") 
        print "except 2" 
 
 
# some information commands (START): 
c.execute("SELECT * from table1") 
print(c.fetchall()) 
c.execute("SELECT * from table2") 
print(c.fetchall()) 
# some information commands (END) 
 
 
#c.execute("create table ZPATIENT as select * from table1 union select * from table2") ---> first union action but some entries duplicated (one column changed?) 
 
# remove some duplicated entries... 
c.execute("create table ZPATIENT as select * from (select * from table1 union select * from table2) final group by ZDATECREATED") 
c.execute("CREATE TABLE Z_PRIMARYKEY (Z_ENT int, Z_NAME text, Z_SUPER int, Z_MAX int)") 
c.execute("CREATE TABLE Z_METADATA (Z_VERSION int, Z_UUID text, Z_PLIST BLOB)") 
 
c.execute("SELECT count(*) FROM ZPATIENT") 
result=c.fetchone() 
number_of_rows=result[0] 
print number_of_rows 
start = 0 
end = number_of_rows + 1 
 
c.execute('SELECT * FROM ZPATIENT')  
newresult=c.fetchall() 
 
for row in newresult: 
 
    start += 1 
    end -= 1 
    print start 
    print end 
 
    # some information commands (START): 
    list_of_tuple = list(row) 
    list_of_tuple[0] = start 
    list_of_tuple[2] = end 
    row = tuple(list_of_tuple) 
    print row 
    # some information commands (END) 
 
    c.execute("UPDATE ZPATIENT SET Z_PK = ? WHERE rowid = ?", (start, start)) 
    c.execute("UPDATE ZPATIENT SET Z_OPT = ? WHERE rowid = ?", (end, start)) 
 
 
c.execute("INSERT INTO Z_PRIMARYKEY (Z_ENT, Z_NAME, Z_SUPER, Z_MAX) VALUES (0, 'Patient', 0, ?)", (start,)) 
 
# close 
conn.commit() 
conn.close() 

为了工作,我在同一个文件夹中有两个名为 store1.data 和 store2.data 的 sqlite 数据库...

如果有人有解决方案...我不知道是否有更简单的方法可以一键执行?

请您参考如下方法:

一个简单的解决方案可能是避免使用 automator,而只是制作一个 bash 脚本来调用 python 脚本。如果需要,您可以通过双击来执行 bash 脚本。

#! /bin/bash 
 
python scriptname.py 

就是您所需要的。