0

I have to write a python script to find available space in /var/tmp folder in linux. So I am using awk command to filter out only available space.

import subprocess
subprocess.call("$(awk 'NR==2 {print $4}' file1.txt)")

The output should be 4.7G but it comes /bin/sh: 1: 4.7G: not found and the return value is 127 and not 0.

3
  • Here at Stack Overflow we do not accept screenshots of code, output and data. Please edit your question and add the outout as code-formatted text. Commented Mar 19, 2020 at 13:41
  • 1
    Why would you do this? Python is perfectly capable of opening files and string processing. Write a function. Commented Mar 19, 2020 at 13:45
  • Please copy&paste the output as text instead of showing a screenshot. The code in the image and the code shown as text in the question should match. Commented Mar 19, 2020 at 14:06

1 Answer 1

3

Apart from the fact that python should be sufficient to do what awk does here, what you are doing with

$(awk 'NR==2 {print $4}' file1.txt)

is to run the awk command and expand its output as part of the command line by $(...). As this is the only part of the command line, the shell tries to execute awk's output as a command.

If you really want to run awk from your python script, remove $( and ) from your command.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Bodo. It was really helpful. I am new to python scripting as well as linux. I was trying to figure out to store the output of awk command in a variable but it stores the return value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.