How can I run terminal commands in python

0 votes

I am trying to create a python script which will ease my daily tasks. One of this task requires me to use the cat, ls commands. How can I run these Linux commands in python?

Feb 4, 2019 in Python by Ganesh
1,267 views

1 answer to this question.

0 votes

You can use the subprocess or os module to do this.

Using subprocess:

import subprocess
subprocess.run(["ls"])

Using os:

import os
os.system("ls")
answered Feb 4, 2019 by Omkar
• 69,220 points

Related Questions In Python

0 votes
0 answers

i want to ask that how can i run one file of python in another file in jupyter notebook

motion_detection.ipynb # Python program to implement # Webcam Motion ...READ MORE

Dec 16, 2020 in Python by Ramsha
• 120 points
862 views
+3 votes
7 answers

How can I rename a file in Python?

yes, you can use "os.rename" for that. ...READ MORE

answered Mar 31, 2018 in Python by DareDev
• 6,890 points
20,083 views
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
1,339 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,720 points
13,593 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,470 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,128 views
–1 vote
2 answers
+4 votes
8 answers

How can I use Python's range function?

The range function is mostly used in for-loop.  Ex: for ...READ MORE

answered Aug 21, 2018 in Python by Omkar
• 69,220 points
2,197 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP