0

How I can interaction with linux console in python. Open (or not) window with console, and do any command or just typing something and press enter

kira
  • 23
  • 3
  • 4
    Does this answer your question? [Running shell command and capturing the output](https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output) – costaparas Dec 31 '20 at 03:15

2 Answers2

1

For example:

import os
os.system('ls')
Rob Kwasowski
  • 2,690
  • 3
  • 13
  • 32
0

I like to use both subprocess and os.
These python modules dont necessarily 'open a console and run a command' rather they interface with your shell.

import os
import subprocess

eg

fileCount = int(subprocess.check_output('ls dir/ | wc -l', shell=True).decode().strip())

or

pathToOldest = './someDir/' + oldestFileInDir
os.remove(pathToOldest)

If youre looking to do run a particular command I would go to a search engine and type 'python run shell commmands' or 'run {enter shell command} in pythonX}

Kevin Crum
  • 195
  • 2
  • 16
  • 1
    https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output – kira Dec 31 '20 at 02:29