簡體   English   中英

python中的子進程調用

[英]subprocess call in python

我需要在終端中為多個文件執行以下命令SetFile -c "" -t "" <FileName>所以我創建了一個python腳本,它將文件名作為參數並使用調用函數來執行命令。 但是我不知道如何在呼叫管道中放置這些“”標記。 這是代碼

from subprocess import call
import sys # for sys.argv
def main():
    l = len(sys.argv)
    l = l - 1
    if(l>0):
        for i in range(l):
            termExecute(sys.argv[i])

def termExecute(argument):
    call(["SetFile", "-c ","","-t ","","argument"])

if __name__ == '__main__':
    main()

我非常確定call(["SetFile", "-c ","","-t ","","argument"])是錯誤的,我希望知道正確的編寫方式。

編輯:

Akuls-MacBook-Pro:~ akulsanthosh$ python3 /Users/akulsanthosh/Documents/Simple/Setfile.py /Volumes/akul/Stuff/1.jpg /Volumes/akul/Stuff/2.jpg /Volumes/akul/Stuff/3.jpg 
Invalid type or creator value: '""'
Invalid type or creator value: '""'
ERROR: File Not Found. (-43)  on file: argument 
Invalid type or creator value: '""'
Invalid type or creator value: '""'
ERROR: File Not Found. (-43)  on file: argument 
Invalid type or creator value: '""'
Invalid type or creator value: '""'
ERROR: File Not Found. (-43)  on file: argument

call(["SetFile", "-c ",'""',"-t ",'""',"argument"])

Python將'"視為有效的字符串定界符,因此這是可能的。即使如此,您也可以轉義引號。實際上,您在代碼中使用了帶有'定界符'__main__'字符串。

查看錯誤后,您會嘗試以下操作
call(["SetFile", "argument"])

根據文檔 ,您可以傳遞空字符串:

args是所有調用所必需的,並且應為字符串或程序參數序列。 通常最好提供一個參數序列,因為它允許模塊處理任何必需的參數轉義和引用(例如,允許在文件名中保留空格)。 如果傳遞單個字符串,則外殼程序必須為True(請參見下文),否則該字符串必須簡單地命名要執行的程序而無需指定任何參數。

您還可以傳遞引號:“”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM