Find in ipython3

random832 at fastmail.us random832 at fastmail.us
Thu Jun 4 16:13:52 EDT 2015


On Tue, Jun 2, 2015, at 12:13, Cecil Westerhof wrote:
> I am thinking about using ipython3 instead of bash. When I want to
> find a file I can do the following:
>     !find ~ -iname '*python*.pdf'
> but is there a python way?

Python really isn't a good substitute for a shell, but the normal python
way to do this task is:

import os, os.path, fnmatch

home = os.path.expanduser('~')  # only needed since you used ~
for dirpath, dirnames, filenames in os.walk(home):
    print(dirpath)
    for filename in filenames:
        if(fnmatch.fnmatch(filename.lower(), '*python*.pdf')):
            print(os.path.join(dirpath, filename))

Note that if you have filenames with invalid unicode characters (or any
non-ASCII characters at all on Windows) you may have to do additional
processing to the filename before printing it. And of course instead of
printing it you may want to store the filenames in a list for further
processing. But these are the basic building blocks.

I don't use ipython, so I don't know what it provides if anything to
make any of this easier.



More information about the Python-list mailing list