Тёмный

python | subprocess module| subprocess.Popen| run OS command using subprocess 

Code4You
Подписаться 353
Просмотров 59 тыс.
50% 1

This video will explain about running OS command using subprocess module.
In this module, we are using subprocess.Popen.
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions:
you can download code from my github link: github.com/happycodingforyou/...
class subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, encoding=None, errors=None)
If shell=True, on POSIX the executable argument specifies a replacement shell for the default /bin/sh.
Popen.wait(timeout=None) : Wait for child process to terminate.
Popen.returncode : A None value indicates that the process hasn’t terminated yet.
A negative value -N indicates that the child was terminated by signal N (POSIX only).

Опубликовано:

 

11 мар 2021

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 36   
@deepikapant9103
@deepikapant9103 3 года назад
Explained so clearly all aspects of subprocess
@zName1
@zName1 2 года назад
Helped out a lot since the information I was finding elsewhere was outdated. Thanks.
@eassonweisshaar9900
@eassonweisshaar9900 8 месяцев назад
Awesome video, just got some work-related stuff working thanks to this video.
@aureliusnt
@aureliusnt 2 года назад
Thanks! Very cool explanation.
@collectiveworld_247
@collectiveworld_247 Год назад
Nice explanation 👍👏
@ganeshpadala3751
@ganeshpadala3751 2 года назад
Excellent video bro ,
@suriyam5740
@suriyam5740 Год назад
Good one bro
@PradeepKumar-jh2gn
@PradeepKumar-jh2gn Год назад
Good one!
@jhenry4948
@jhenry4948 5 месяцев назад
Can you run it from inside your IDE and it up the terminal print to the terminal in real time
@ragtop63
@ragtop63 Год назад
Good video. However, I have read multiple posts regarding the use of this module and almost all of them recommend against using shell=True. What are your thoughts?
@mayconfelipe4981
@mayconfelipe4981 2 года назад
nice video
@anzwertree
@anzwertree 2 года назад
Could I used this to monitor the breads I make, giving me a live counting list of each loaf I made throughout the day, so I can tell when one of the loaves is ready to cut and serve? Each bread needs to cool for up to 2 hours. So I'd like to be able to keep adding breads to an ever expanding list. That way I can just look at it and tell how long each batch of bread has been cooling. I'm a baker. Thanks
@bennguyen1313
@bennguyen1313 7 месяцев назад
If in windows, I do: type logfile | cantools plot inputfile.dbc What would the syntax look like if I'm trying to do using Popen? The following did not work as it did not pipe the logfile contents to cantools args = ["plot 1.dbc"] p = Popen(['cantools'] + args, stdout=PIPE)
@nk_nicolasmayoral
@nk_nicolasmayoral 2 года назад
And in case if i want to save in a file, the standard out and the error? For example: -c command -f file-out -l log-file . How can i do in that case?
@venkata1857
@venkata1857 Год назад
Nice explanation..... Is there any alternate library module to run with python 2.7 version
@PGhai
@PGhai 2 года назад
Nice video, but your heading is misleading, I was looking for subprocess.run
@pradyumnakatageri3475
@pradyumnakatageri3475 2 года назад
Thank you for the explanation!. I have one doubt. Suppose if I want to execute a command with multiple arguments to be passed in the CLI using the Popen subprocess. For Eg. jira --action createIssue --project "$1" --type "$2" --description $3 I get an error stating that the bufsize must be an integer. But when I provide a valid value for the bufsize argument in Popen. I still get an error stating too many values passed for bufsize argument
@code4you753
@code4you753 2 года назад
May be when you are passing the argument it is taking as char. You may need to cast your value as integer and try. eg: suppose your are passing $1 then first_num = int(sys.argv[1])
@danielgeorge5239
@danielgeorge5239 6 месяцев назад
Say I wanted to run ssh in a subprocess as a remote shell?
@anuragmishra1800
@anuragmishra1800 2 года назад
How can i read the output of long running process without pausing the program
@RelaxingMusic-dt4cr
@RelaxingMusic-dt4cr Год назад
please can anyone advise how can I use Atom with auto-completion same as you do..I'm using Atom on MAC OS
@pravalikakashineni2196
@pravalikakashineni2196 2 года назад
Do more videos
@gesseanedejesusgomes349
@gesseanedejesusgomes349 3 года назад
Good night teacher! Would you have any commands to verify that the person ran the application as an administrator?
@code4you753
@code4you753 3 года назад
Usually application should handle and authenticate if running by adminstrator or not. If you are considering python code as application and your running on window then you can check via ctypes.windll.shell32.IsUserAnAdmin() ==> 0 ## running via normal user 1 means running by adminstrator. For unix, you can use os module or getpass.getuser(). You may combine also.
@Jonix-redhat
@Jonix-redhat 3 года назад
Hi! if I run a bioinformatics pipeline, will this method show the live feed of the pipeline and also capture stderr if something goes wrong in the pipeline? today I have tried using subprocess.run, but if I use the capture_output=True, I wont get any live feed. and if I skip that I will get live feed but I wont get any stderr so I can see what went wrong in case it does:/ pls help!:)
@code4you753
@code4you753 2 года назад
in some cases, your error will captured in stdout also. Believe, you are capturing stdout and stderr also. Please check value of both. You will get required info. To know if your subprocess completed or not, You can check with returncode. If returncode == 0 then success else fail.
@prashanthb6521
@prashanthb6521 11 месяцев назад
In the SULTAN package you have something like Streaming=True for this purpose.
@hirakhax
@hirakhax 2 года назад
What is the ubuntu version u are using?
@yousen5678
@yousen5678 3 года назад
hey, please how i can rexecute my script python an intern if it's stopped ?
@code4you753
@code4you753 2 года назад
you mean to say subprocess failed then you want to execute once again ? You can write via n-number ways. I am giving you one example using function. if success then returncode will return 0 else other than 0. You can loop through. You can add stdout and stderr. If you want capture value of stdout and stderr. ================================ import subprocess def run_os_cmd(cmd): p1 = subprocess.Popen(cmd, s p1.wait() return p1.returncode cmd = "du -sh /tmp" cmd_status = run_os_cmd(cmd) if cmd_status != 0: cmd_status = run_os_cmd(cmd)
@zahidriaz371
@zahidriaz371 Год назад
give example open other then cmd and then process onit.
@surajjoshi3433
@surajjoshi3433 2 года назад
subprocess only runs on windows??
@ExploitHunter
@ExploitHunter 2 года назад
Make py keylogger
@amosburton4728
@amosburton4728 2 года назад
Why is every youtube educational video produced out of India such low quality?
@Heynmffc
@Heynmffc Год назад
This one isn’t that bad? And I think we’re just spoiled in the west.
Далее
Python Call a System Command!
10:14
Просмотров 24 тыс.
다리에 힘이 풀려버린 슈슈 (NG Ver.)
00:11
Просмотров 1,8 млн
Simulating the Evolution of Rock, Paper, Scissors
15:00
FANG Interview Question | Process vs Thread
3:51
Просмотров 290 тыс.
Understanding stdin, stdout, stderr in Python
11:53
Просмотров 11 тыс.
Automate your job with Python
6:07
Просмотров 379 тыс.
Simple Automated SSH Python Bot
17:50
Просмотров 30 тыс.
Practical introduction to Python's subprocess module
52:46