简体   繁体   中英

How to run a function in another python script with arguments?

I'm trying to make a script that runs another python script with a few different commands. I want to authenticate a user and search a song given by the user. Then the script authenticates the spotify account, searches for the given song and then plays it.

Here's how it looks when to authenticate Respotify the moment:

python respotify.py john doe

Searching for a song:

search Wrecking Ball

Play a song after a specified number of search The results will be:

play 1

I know what I could call the function using this for example in my play.py file:

import os
os.system = ("search Wrecking Ball")

But I want my "play.py" to run the respotify.py in the background. I've tried with putting this inside a respotify.py function so that maybe a song could be called as the script starts.

import os 

os.system = "search Wrecking Ball"

I tried but my script would just ignore it.

But I think that it's more complicated than that. You can find respotify.py here :

Close on solving my problem now thanks to nickie

His answer was:

from subprocess import Popen, PIPE

p = Popen(["python", "respotify.py", "john", "doe"], stdin=PIPE, stdout=PIPE)
print >> p.stdin, "search Wrecking Ball"
output = p.communicate()[0]

But I got this error in my console:

File "respotify.py", line 338, in <module> command_loop() File "respotify.py", line 29, in command_loop command = raw_input().split(" ") EOFError: EOF when reading line

How do I fix this error above?

Both os.system and subprocess.call execute a program in a subprocess, as if from the command line. If I understand right, you want to execute the command:

python respotify.py john doe

and feed it with input, as if given to its stdin , that will contain the line:

search Wrecking Ball

This can be done with the following code:

from subprocess import Popen, PIPE

p = Popen(["python", "respotify.py", "john", "doe"], stdin=PIPE, stdout=PIPE)
input = "search Wrecking Ball\n" + "play 1\n"
output = p.communicate(input)[0]

or, if the input that you want to provide depends on the output that the subprocess is giving you, you could print to p.stdin and read from p.stdout .

from subprocess import Popen, PIPE

p = Popen(["python", "respotify.py", "john", "doe"], stdin=PIPE, stdout=PIPE)
print >> p.stdin, "search Wrecking Ball"
output = p.stdout.readline()
print >> p.stdin, "play 1"
print >> p.stdin, "quit"
p.wait()

Check the documentation of the subprocess module for further details.

If the communication protocol with your subprocess is complicated, you should absolutely use something better than this, like the pexpect library that @abernert suggested.

Trying to interact with a CLI script—send commands, wait for responses and prompts, etc.—is a pain in the neck. There are libraries like pexpect that make it easier for you, but it's still not easy.


But fortunately, you don't have to. respotify is clearly usable as a module instead of a script, so, rather than try to generate input to feed it so it can parse it, and so on, just import it and use it directly.

Or, even more simply, all of the code in respotify is simple—at least as simple as trying to interact with it over a CLI. So just use the Spotify modules directly.

os.system = ("search Wrecking Ball")

Doesn't do what you think it does. You want

output = os.system("search Wrecking Ball")

Note that the modern way to do this is

output = subprocess.call(["search", "Wrecking", "Ball"])

If you can possibly avoid it, PLEASE do not make your Python scripts run other Python scripts via subprocess or via pexec, et.al. These are last-ditch options "from an Older Age".

Python gives us the wonderful gift of just import ing another Python script as a library and calling it directly.

If you possibly can, import respotify and call its methods directly. If not, "library-itize" it so that it can be called directly. Your time and sanity will thank you for it.

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