1

I am trying this on python:

import os

process="find . -type f -name *.out*.xyz'"
print "the number of xyz files is %s" %os.system(process)

When i try it from command line it works, but from python it returns the number 0, when it is 4. I think there must be a problem with the use of os.system. Any help?

2
  • 2
    Also check out the subprocess module. It's prefered over os.system these days. Commented Aug 4, 2017 at 10:23
  • And by "these days", we mean "since its release nearly 13 years ago". Commented Aug 4, 2017 at 11:46

2 Answers 2

2

Yes what you are getting is the return code "0" meaning "successfully completed, no errors" and not the stdout text output "4".

You should look here to get the output: Assign output of os.system to a variable and prevent it from being displayed on the screen

And here to learn about POSIX exit codes: https://en.wikipedia.org/wiki/Exit_status

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

Comments

0

Actually, if you really just need the number of files in Python, try this:

import pathlib
p = pathlib.Path('.')
files = [x for x in p.iterdir() if x.is_file() and x.match('*.csv')]
print("Number of files: {0}".format(len(files)))

Easier, Pythonic and you do not need to fork a sub-process.

Comments

Your Answer

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