[英]How do I input strings in Linux terminal that points to file path using subprocess.call command?
I'm using Ubuntu and have been trying to do a simple automation that requires me to input the [name of website] and the [file path] onto a list of command lines. 我正在使用Ubuntu,并且一直在尝试做一个简单的自动化操作,要求我在命令行列表中输入[网站名称]和[文件路径]。 I'm using subprocess and call function.
我正在使用子过程和调用功能。 I tried something simpler first using the "ls" command.
我首先使用“ ls”命令尝试了一些更简单的方法。
from subprocess import call
text = raw_input("> ")
("ls", "%s") % (text)
These returned as "buffsize must be an integer". 这些返回为“ buffsize必须是整数”。 I tried to found out what it was and apparently I had to pass the command as a list.
我试图找出它是什么,显然我不得不将命令作为列表传递。 So I tried doing it on the main thing im trying to code.
因此,我尝试在主要尝试编写代码的方面进行此操作。
from subprocess import call
file_path = raw_input("> ")
site_name = raw_input("> ")
call("thug", -FZM -W "%s" -n "%s") % (site_name, file_path)
These passed as an invalid syntax on the first "%s". 它们在第一个“%s”上作为无效语法传递。 Can anyone point me to the correct direction?
谁能指出我正确的方向?
Here is a complete example of how you would call a executable python file with subprocess.call
Using argparse
to properly parse the input. 这里是你将如何调用一个可执行文件的Python一个完整的例子
subprocess.call
使用argparse
正确解析输入。
Your python file to be called (sandboxArgParse.py): 您要调用的python文件(sandboxArgParse.py):
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--filePath", help="Just A test", dest='filePath')
parser.add_argument("--siteName", help="Just A test", dest='siteName')
args = parser.parse_args()
print args.siteName
print args.filePath
Your calling python file: 您的调用python文件:
from subprocess import call
call(["python","/users/dev/python/sandboxArgParse.py", "--filePath", "abcd.txt", "--siteName", "www.google.com"])
You cannot use %
on a tuple. 您不能在元组上使用
%
。
("ls", "%s") % text # Broken
You probably mean 你可能是说
("ls", "%s" % text)
But just "%s" % string
is obviously going to return simply string
, so there is no need to use formatting here. 但是,仅
"%s" % string
显然会返回简单的string
,因此此处无需使用格式。
("ls", text)
This still does nothing useful; 这仍然无济于事。 did you forget the
call
? 你忘了
call
吗?
You also cannot have unquoted strings in the argument to call
. 您也不能在参数
call
包含未加引号的字符串。
call("thug", -FZM -W "%s" -n "%s") % (site_name, file_path) # broken
needs to have -FZM
and -W
quoted, and again, if you use format strings, the formatting needs to happen adjacent to the format string. 需要用引号
-FZM
和-W
再次声明,如果您使用格式字符串,则格式需要在格式字符串旁边进行。
call(["thug", "-FZM", "-W", site_name, "-n", file_path])
Notice also how the first argument to call()
is either a proper list, or a long single string (in which case you need shell=True
, which you want to avoid if you can ). 还要注意
call()
的第一个参数是一个适当的列表还是一个长字符串(在这种情况下,您需要shell=True
, 如果可以的话 ,应避免使用 )。
If you are writing new scripts, you most definitely should be thinking seriously about targetting Python 3 (in which case you want to pivot to subprocess.run()
and input()
instead of raw_input()
too). 如果你正在写新的剧本,你最绝对应该认真考虑靶向Python 3中(在这种情况下,你要转动到
subprocess.run()
和input()
代替raw_input()
太)。 Python 2 is already past its originally announced end-of-life date, though it was pushed back a few years because Py3k adoption was still slow a few years ago. Python 2已经过了最初宣布的寿命终止日期,尽管由于几年前Py3k的采用仍然很慢,所以它被推迟了几年。 It no longer is, and shouldn't be -- you want to be in Py3, that's where the future is.
它不再是而且不应该-您想进入Py3,那就是未来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.