0

I am new to python, and I am converting a C++ file into python, but I was not sure how to translate the following lines of C++ code:

virtual void show() const override
{ system(("cat " + filename).c_str()); }
Lundin
  • 155,020
  • 33
  • 213
  • 341
  • I know there is "os" method which can be used but I don't know how? – user2483882 Jun 25 '13 at 23:43
  • 12
    Funny, I copy-pasted your title in Google and got a bunch of results that answer your question. – i Code 4 Food Jun 25 '13 at 23:43
  • Poorly written title. You probably meant to ask "What is the Python equivalent of system() in C++?" – 2rs2ts Jun 25 '13 at 23:46
  • 3
    @2rs2ts - "what does `system` do" is a perfectly proper question, and a reasonable one for this problem. Don't get snarky over grammar issues; not everyone speaks and writes English as well as you think you do. – Pete Becker Jun 26 '13 at 00:18
  • 1
    @PeteBecker I appreciate you defending those whose English is confusing. We have a lot of users for whom English is a second or third language, and it's important to give them the benefit of the doubt. However, I wasn't being picking on someone else's grammar. I was only considering the body of the question. OP wanted to translate some C++ code to Python, and he asked "What does 'system' in c++" which led me to believe he wanted to know what it did so he could translate it to Python; in other words, he wanted to know the equivalent of `system()` is in Python. Or so I assumed; hence "probably." – 2rs2ts Jun 26 '13 at 00:38
  • http://linux.die.net/man/3/system – kfsone Jun 26 '13 at 01:32
  • 2
    Also note that *translating* code from one language to another is generally a really bad idea - code that is sensible in one language often makes for terrible design in another. C++ and Python are very different languages, *translating* like this will generally result in unpythonic, inefficient, hard to read code. Instead, read the C++ code, figure out what it's intended to do, then implement *that functionality* (not the equivalent to *that code*) in Python. – Gareth Latty Jun 26 '13 at 13:29

2 Answers2

11

system() is a function which executes a system command.

Doing this in Python is usually performed with the subprocess module. See this question for more information.

The docs even have a section showing how to replace the system call:

subprocess.call("cat " + filename, shell=True)

Python does have a function os.system that's does exactly the same thing as the C function, because it's a thin wrapper around that C function:

This is implemented by calling the Standard C function system(), and has the same limitations.

And you can easily verify in the source that this is true.

But as the docs say, "The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function."

Community
  • 1
  • 1
2rs2ts
  • 9,020
  • 6
  • 44
  • 80
  • An edit to my post was suggested, but it was rejected for changing too much. But, it had some good points. As @Kyllopardiun shows, you can use `os.system()` to perform this call too; it seems to be the rough equivalent. I recommend `subprocess` nonetheless, but, references are not bad things. – 2rs2ts Jun 26 '13 at 00:44
  • 3
    It's not just "the rough equivalent"; it's just a thin wrapper around the C function, so it's an _exact_ equivalent. – abarnert Jun 26 '13 at 00:59
  • 2
    Of course, in Python you can call the program directly as well: `subprocess.call(["cat", filename], shell=False)`. This avoids problems with clever users that set `filename` to things like `"/dev/null;rm -rf *"` – Mike DeSimone Jun 26 '13 at 01:26
  • @abarnert Didn't know it was a wrapper. Thanks for the clarification. – 2rs2ts Jun 26 '13 at 16:19
1

be sure to import os "equivalent" to < cstdlib > < stdlib.h > where the function system() is normally implemented.

An exact conversion could looks like:

os.system(str("cat" + filename)), however if filename is already a string, then just call as: os.system("cat" + filename)

Also, I am not so familiar with C++, but knowing ANSI C, it seems like virtual void show() is some method or function therefore you might be looking for:

def show(filename):
   os.system("cat" + filename)
#notice that i used the filename as parameter,
#however if its a global variable this is unnecessary.

An important advise: it might be implemented inside a class, if the filename variable is "global" or public for methods in this class, then the parameters is not needed.

Good luck!

ColorCodin
  • 81
  • 2
  • 11
Mansueli
  • 4,463
  • 6
  • 27
  • 50