Python check output windows

Check output of subprocess python3

hey i’m updating my script , I’ve changed os to subprocess for now, but i don’t know how to check the output? I want to check is it empty or what its in. Script just execute other program or list of sentences.

1 Answer 1

When you start a process it has automatically open 3 file descriptors (you can look at it like on one-way communicating channel):

  • STDIN — Used for input from user (e.g. used when entering password to SSH)
  • STDOUT — The text which is used as standard otuput of the application (e.g. channel for printing messages on console)
  • STDERR — Same as STDOUT but used fo error messages.

You can redirect output (also input but that doesn’t apply to your question) to files (in other words you «disconnect» the console and «connect» a file). For redirecting are specified those symbols: > and >> (for more information look at the https://unix.stackexchange.com/questions/171025/what-does-do-vs). The command ls -l >> file.txt will write output of ls -l and write anything on the console (because it’s «disconnected»)

The result = subprocess.check_output(. ) takes commannd and «connect» its STDOUT into the result variable. But you are running this command: esearch -db protein -query %s | efetch -format fasta >> results_seq.txt and if you look at the end you’ll see >> results_seq.txt . So output of your command is already «connected» into file results_seq.txt and cannot be saved inside the result variable.

Now you can use those approaches:

Run your command as is and then open file results_seq.txt and look at the content

Do not use redirection in the command and save it on your own in Python:

Use tee which is utility allows redirect output to file and also keep it on console

At the end of any of those examples you will have saved output of your command saved in the variable result

How do you use subprocess.check_output() in Python?

I have found documentation about subprocess.check_output() but I cannot find one with arguments and the documentation is not very in depth. I am using Python 3 (but am trying to run a Python 2 file through Python 3)

Читайте также:  Установщик модулей windows не запускается

I am trying to run this command: python py2.py -i test.txt

-i is a positional argument for argparse, test.txt is what the -i is, py2.py is the file to run

I have tried a lot of (non working) variations including: py2output = subprocess.check_output([str(‘python py2.py ‘),’-i’, ‘test.txt’])

py2output = subprocess.check_output([str(‘python’),’py2.py’,’-i’, test.txt’])

3 Answers 3

The right answer (using Python 2.7 and later, since check_output() was introduced then) is:

To demonstrate, here are my two programs:

Here’s what’s wrong with each of your versions:

First, str(‘python py2.py’) is exactly the same thing as ‘python py2.py’ —you’re taking a str , and calling str to convert it to an str . This makes the code harder to read, longer, and even slower, without adding any benefit.

More seriously, python py2.py can’t be a single argument, unless you’re actually trying to run a program named, say, /usr/bin/python\ py2.py . Which you’re not; you’re trying to run, say, /usr/bin/python with first argument py2.py . So, you need to make them separate elements in the list.

Your second version fixes that, but you’re missing the ‘ before test.txt’ . This should give you a SyntaxError , probably saying EOL while scanning string literal .

Meanwhile, I’m not sure how you found documentation but couldn’t find any examples with arguments. The very first example is:

That calls the «echo» command with an additional argument, «Hello World!» .

-i is a positional argument for argparse, test.txt is what the -i is

I’m pretty sure -i is not a positional argument, but an optional argument. Otherwise, the second half of the sentence makes no sense.

Устаревшие функции модуля subprocess в Python.

До Python-3.5 эти три функции включали высокоуровневый API для модуля subprocess . Теперь вместо них можно использовать функцию subprocess.run() , но многие существующие коды все еще вызывают устаревшие функции, которые перечислены ниже.

Содержание:

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None) :

Функция subprocess.call() запускает команду, описанную аргументами args , дожидается завершения команды, а затем возвращает кода завершения программы returncode .

Вместо функции subprocess.call() теперь необходимо использовать универсальную функцию subprocess.run() следующим образом:

Чтобы подавить вывод stdout или stderr , укажите значение соответствующих аргументов равным subprocess.DEVNULL , например stdout=subprocess.DEVNULL .

Функция subprocess.call() передает все свои аргументы напрямую в интерфейс subprocess.Popen() , кроме времени ожидания timeout .

Примечание.

  • Не используйте stdout=subprocess.PIPE или stderr=subprocess.PIPE с этой функцией. Дочерний процесс блокируется, если он генерирует достаточно данных для канала, тем самым заполняя буфер канала ОС.
subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None) :

Функция subprocess.check_call() запускает команду с аргументами, дожидается завершения команды и возвращает 0 если код завершения программы был равен нулю, в противном случае вызывает исключение CalledProcessError . Объект CalledProcessError будет иметь код завершения в атрибуте returncode .

Читайте также:  Установщик обнаружил ошибку 0x80240037 windows

Вместо функции subprocess.check_call() теперь необходимо использовать subprocess.run() следующим образом:

Чтобы подавить вывод stdout или stderr , укажите значение соответствующих аргументов равным subprocess.DEVNULL , например stdout=subprocess.DEVNULL .

Функция subprocess.check_call() передает все свои аргументы напрямую в интерфейс subprocess.Popen() , кроме времени ожидания timeout .

Примечание.

  • Не используйте stdout=subprocess.PIPE или stderr=subprocess.PIPE с этой функцией. Дочерний процесс блокируется, если он генерирует достаточно данных для канала, тем самым заполняя буфер канала ОС.
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, cwd=None, encoding=None, errors=None, universal_newlines=None, timeout=None, text=None) :

Функция subprocess.check_output() запускает команду с аргументами и возвращает ее вывод.

Если код возврата был ненулевым, то функция вызывала исключение CalledProcessError . Объект CalledProcessError будет иметь код завершения в атрибуте returncode и любые выходные данные в атрибуте output .

Теперь функция subprocess.check_output() эквивалентна следующему коду:

Полная сигнатура функции subprocess.check_output() в значительной степени совпадает с сигнатурой subprocess.run() — большинство аргументов передаются напрямую в этот интерфейс. Однако явная передача input=None для наследования стандартного дескриптора входного файла родителя не поддерживается.

По умолчанию эта функция возвращает данные в виде закодированных байтов. Фактическое кодирование выходных данных может зависеть от вызываемой команды, поэтому часто требуется декодирование в текста на уровне приложения.

Adding timeout in subprocess.check_output

I am developing a small tool in Python 2.7 and using subprocess module. I am using this module to run commands on remote devices using its check_output function. There might be a situation in which the remote device is not functioning and therefore I am getting the following response: Timeout: No Response from 10.xxx.xxx.xxx Following is my code:

I want to put timeout into this function so that if after a certain amount of time, no response is recieved, my code goes in the Exception part and prints the given message. I tried using timeout argument in the check_output command but after running my script with timeout argument, it is immediately printing the message given in the Exception part. What I tried:

2 Answers 2

Python 2.7 does not support timeout parameter. You can instead use EasyProcess. This is a layer on top of subprocess module and pretty easy to use.

My guess is that you are running your code in Python 2.

If that is the case, subprocess.check_output() does not accept a timeout parameter, and the function will fail immediately with:

But, because you are catching all exceptions and printing a generic message, you don’t see the actual exception, and you assume that the command is timing out immediately.

Читайте также:  Xerox b205 driver windows

One way to fix this problem is to run your code in Python 3.

Whether you are running Python 2 or 3, I recommend that you do not catch all exceptions, or that you at least print the value of the exception so that you can see the actual cause, e.g.

which explicitly checks for a timeout exception. All other exceptions are propagated as usual and so are not masked by your «catch all» code. Or,

but the former is preferred.

Edit

OP can’t use Python 3. If you are using Linux then you could use the timeout command, e.g.

On timeout this will raise an exception with a particular exit status value of 124:

BTW you shouldn’t use the shell=True option as there are security implications as mentioned in the documentation. Instead you should pass a list of strings to check_output() like this:

If you are using another OS (or you don’t want to use timeout ) then you can run your subprocess in a separate thread and have your main thread time it out if required. See this other question, Using module ‘subprocess’ with timeout, for details about how to do that.

check_output error in python

I am gettin a error while running the below code.

Error

I am working on Python 2.6.4 .

3 Answers 3

You’ve defined your own function, it’s not an attribute of subprocess module(for Python 2.6 and earlier).

You can also assign the function to the imported module object(but that’s not necessary):

You probably just want to use check_output , but, just so you know, there is a method subprocess.check_output , but it’s not defined until Python 2.7 (http://docs.python.org/3/library/subprocess.html#subprocess.check_output)

You might even want this, which defines the function in the module if it’s not there (i.e. running before v2.7).

try the following:

ensure that you have \ in your path rather than ‘/’ and «dir» rather «ls»

Not the answer you’re looking for? Browse other questions tagged python python-2.6 or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Оцените статью