0

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. I'm using subprocess and call function. I tried something simpler first using the "ls" command.

from subprocess import call
text = raw_input("> ")
("ls", "%s") % (text)

These returned as "buffsize must be an integer". 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". Can anyone point me to the correct direction?

AFME
  • 3
  • 1

2 Answers2

0

Here is a complete example of how you would call a executable python file with subprocess.call Using argparse to properly parse the input.

Your python file to be called (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:

from subprocess import call


call(["python","/users/dev/python/sandboxArgParse.py", "--filePath", "abcd.txt", "--siteName", "www.google.com"])
user1767754
  • 23,311
  • 18
  • 141
  • 164
  • I completely agree that parsing command-line arguments is **way** better than interactive input, but I don't think this answer is going to help the OP. – tripleee Nov 28 '17 at 06:13
  • :D That's why he have you @tripleee!. I'm giving an alternative solution that is cleaner. Your answer will probably solve the problem, my answer might be a motivation to do things with beauty. – user1767754 Nov 28 '17 at 06:17
0

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.

("ls", text)

This still does nothing useful; did you forget the call?

You also cannot have unquoted strings in the argument to 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.

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).

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 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. It no longer is, and shouldn't be -- you want to be in Py3, that's where the future is.

tripleee
  • 175,061
  • 34
  • 275
  • 318