forAllBright
forAllBright

Reputation: 61

Using python with bash

import os
ot = os.popen("%s") %"ls"
TypeError: unsupported operand type(s) for %: 'file' and 'str'

I can not figure it out why error occurs. I mean it's pure string operation, right? Any help could be appreciated.

Upvotes: 0

Views: 64

Answers (1)

dawg
dawg

Reputation: 103814

Python is great because of the interactive shell.

Try:

>>> import os
>>> os.popen("%s")
<open file '%s', mode 'r' at 0x10d020390>

You can see the error in front of you. The result of os.popen is a file. You are then applying a string operation to that.

Translating what you have to what I think you are trying to do, try:

>>> os.popen("%s" % "ls").read()

Or, directly:

>>> os.popen("ls").read()

But the subprocess module is usually preferred:

>>> import subprocess
>>> subprocess.check_output("ls")

Upvotes: 6

Related Questions