3

I have a php script that I called in command line like that :

$php importTextFile.php --user "X" --title "name" notice_X.txt 

I want to use that file in a python script. I've tried :

for file in os.listdir("."):
    subprocess.call(["php", "-f", "importTextFile.php"], "--user=X", "--title="'%s' % name, file)

I've got he following error :

  File "./pageFromFile.py", line 21, in main
    subprocess.call(["php", "-f","importTextFile.php"], "--user=Bot", "--title="'%s' % nom, fichier)
  File "/usr/lib/python2.7/subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 629, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

I don't understand how to fix this error. Thanks for your help.

edit1 : thanks it works, but I've got problem withs args because the Usage message appeared :

Usage: php [options] [-f] <file> [--] [args...]
       php [options] -r <code> [--] [args...]
       php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
       php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
       php [options] -- [args...]
       php [options] -a

edit2 : I've changed arguments's order and it works : ["php", "php /script /path", "-f", "--user", "X", "--title", X, file ]

1
  • Any reason why you cannot do this in python? Commented Aug 2, 2012 at 15:38

2 Answers 2

5
subprocess.call(["php", "-f", "importTextFile.php", "--user=X", "--title="'%s' % name, file])

Should fix it

Sign up to request clarification or add additional context in comments.

Comments

1

Here's a messy explanation due to messy documentation.

From: http://docs.python.org/library/subprocess.html#frequently-used-arguments

To support a wide variety of use cases, the Popen constructor (and the convenience functions) accept a large number of optional arguments. For most typical use cases, many of these arguments can be safely left at their default values. The arguments that are most commonly needed are:

From: http://docs.python.org/library/subprocess.html#subprocess.call

The signature for call() is:

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

You'll want your CL arguments all within a single list - as Jakob provided. That is the first argument to call().

The other positional arguments correspond to initialization parameters for class subprocess.Popen (http://docs.python.org/library/subprocess.html#subprocess.Popen). bufsize happens to be one of them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.