简体   繁体   中英

How do I incorporate a raw_input as an argument in a function

I'm making a small github script for myself. I'm trying to have the call command with an argument then raw input as another argument. I have no idea how to even start.

  file = raw_input("enter file name:  ")
  call(["command", "argument", "input here"])

How do i add the incorporate the raw input?

You can do this:

file_name = raw_input("enter file name:  ")
call(["command", "argument", file_name])

Please don't use file as variable, it's a python type

And you don't need quotes, because file_name will be a string that you can put directly in your list.

You seem to confuse strings with string-literals. The first one is a sequence of characters (actually strings again in Python), whereas the latter is a way to write such a string within a program.

So

foo = "my string"

does not contain any actual quotes. Eg the length is 9, the first character foo[0] is m and so forth.

raw_input returns a string-object, so if it's content should be passed on, you can just take the variable you assigned it to & and pass it as argument to create a list that in turn you pass to subprocess:

 user_input = raw_input()
 subprocess.check_call(["program", user_input])

For your actual use-case, don't be confused by having to use quotes in the shell for certain use-cases, as these serve a similar purpose there. The shell tokenizes input by spaces, so

 $ command arg1 arg2 arg3

will be 3 arguments for command . But if you need one argument to contain spaces (eg certain filenames with spaces in them), you need to do

 $ command "my spaceful argument"

However, the Python subprocess module (unless you use shell=True ) does not suffer from this problem: there the arguments you pass as list will immediately be passed to the child-process without the need for quotes.

一个简单的解决方案是将raw_input放入调用中:

call(["command", "argument", raw_input("enter file name:  ")])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM