python中的子进程调用 [英] subprocess call in python

查看:32
本文介绍了python中的子进程调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在终端中为多个文件执行以下命令SetFile -c "" -t "" <文件名>所以我创建了一个 python 脚本,它将文件名作为参数并使用调用函数来执行命令.但我不知道如何将这些 "" 标记放在调用管道中.这是代码

I need to execute the following command in terminal for multiple files SetFile -c "" -t "" <FileName> so I created a python script that would take filenames as arguments and use call function to execute the command. But I don't know how to put those "" marks in call pipe. here is the code

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"]) 是错误的我希望知道正确的写作方式.

I am pretty sure the call(["SetFile", "-c ","","-t ","","argument"]) is wrong I hope to know the right way of writing it.

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__' 分隔 在您的代码中.

Python treats both ' & " as valid string delimiters, thus this is possible. Even otherwise you can escape the quotes. In fact you've used string with ' delimits with '__main__' in your code.

查看您收到的错误后,我会尝试以下
call(["SetFile", "argument"])

After looking at the errors you're getting I would try the below
call(["SetFile", "argument"])

这篇关于python中的子进程调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆