How would you make the give code more Pythonic?
I want to get Paths out of the sample like /usr/local/sources/devel/algebra.py: def _alg(...) without the character :.
My code
import os
FunctionPath = "/usr/local/sources/devel/sage-main/build/sage/"
cmd = "grep -R 'def ' %s | cut -d' ' -f1" % (FunctionPath)
cmd += ' &'
raw_path = os.system(cmd)
path = raw_path.replace(':', '') // not working
print path
[edit]: The code cannot be written pythonically only with Built-in functions.
Why not do this:
And if you use fileinput module you can iterate over lines from multiple input streams very easily:
BTW, if you don’t give fileinput.input any path it will use sys.argv by default, so you can run your python script like this:
fileinput will read the files for you.
if you really want to cover all cases, you shouldn’t be using replace(‘:’, ”) because it is possible to have
The following will give you the correct result:
Unless you want the comment as well.