2

I used grep command from shell and it gave the result I wanted but when I run from my python script using os.popen it said

grep: SUMMARY:: No such file or directory

Normal grep command:

grep -A 12 -i "LOGBOOK SUMMARY:" my_folder/logbook.log

Python script

command="grep -A 12 -i LOGBOOK SUMMARY: my_folder/logbook.log"
result=os.popen(command)

Normal grep command gave the result I wanted. 2nd one said no such file or directory

1
  • use the full path to the file Commented Jul 14, 2017 at 0:14

1 Answer 1

4

You need to enclose the search pattern within quotes:

command="grep -A 12 -i 'LOGBOOK SUMMARY:' my_folder/logbook.log"

How to diagnose such problems? Start from the error message:

grep: SUMMARY:: No such file or directory

This error message tells that grep could not find a file named SUMMARY:.

The right question to ask is, why is grep looking for a file named SUMMARY:? And the answer is that on the command line you executed, somehow SUMMARY: is considered a filename:

command="grep -A 12 -i LOGBOOK SUMMARY: my_folder/logbook.log"

Of course! That's what would happen if you executed that command in the shell:

grep -A 12 -i LOGBOOK SUMMARY: my_folder/logbook.log

Here, the shell will split the command line on spaces, and pass to grep 3 arguments, LOGBOOK, SUMMARY: and my_folder/logbook.log. The first argument, LOGBOOK is used as the pattern to search for, and all remaining arguments are taken as filenames to search in.

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

1 Comment

Thanks a lot. Usually people give the solution but the way you explained. Awsome!!

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.