How to find files with a string ++

Avi Gross avigross at verizon.net
Wed Jan 9 21:00:36 EST 2019


[This message comments on three different items being discussed and also on
how to find out what kind of help is actually requested and warranted.]

People don't usually provide enough context in their requests and I see
wildly different attempts to help. When some code is supplied along with
perhaps an error message, some people try to see what may be wrong and
assume they want to use a slightly improved version of the code to solve
whatever their goal is. Others point to where some other code does it
perhaps a very different way such as the code in IDLE. Some tell you to go
elsewhere and use a finished product as below. So, sure, you can just go and
use grep. And some focus on things like efficiency or portability or how
pythonic its is, whatever that means to THEM.

What is missing at times is the question of what do you want to do next or
even before. That may help guide what is wanted.

It is not easy to guess especially since Anton may not be using the data
structures he might if he was more experienced in python. He seems to be
wanting to use a set which may not seem very focused if the file names are
all unique already. Some might use something simpler and ordered like a
list. If his purpose is to do something like show which files do NOT match,
sure he could make a set of all files and subtract this set but that is not
needed as it is quite simple to record just files that don't match in the
first place.

I am left with a few other anomalies that make me think it would be better
to stop and ask what they want in clearer terms because their overall
approach does not lead me to something obvious.

When people post here, there is no way to enforce their telling us their
general goals. It can be anything from a student trying to solve a problem
using very basic methods they are supposed to have covered in class to
someone just trying to get a task done just once and not caring much what
tools is used and hoping someone on this forum has a quick answer.

If someone says they want to code each one of dozens of algorithms they read
about from SCRATCH, that means they are willing to reinvent the wheel. They
may literally not want any comment on any aspect of their code except asking
how to fix the one error they got. In this case, I am not clear if the
answer is to initialize a set rather than a dictionary, or to use a list in
the first place, or if using a dictionary, supply a key. Perhaps they do not
(yet) want to hear their entire function always returns the filename or
maybe it should return an item instead of a set containing an item or ...

I am reading a book about Clean Code and I note that sometimes here we
ignore fairly simple things and fall for a more gimmicky, if brilliant,
attempt to make something non-obvious. The example was one that comes up
regularly, how to see if two items are equal. In this case, the question was
how to tell if a list of N items contained N identical items. A fairly
straightforward answer would be to loop starting with a second item and keep
comparing the current item to the withheld first. Optionally, quit as soon
as a discrepancy was found. It would depend on using a binary "==" on two
items at a time. Others offered more inline solutions that hide a loop, such
as "any" ad many such variations are possible. 

I am not saying they are all good solutions, and many are fairly horrible
such as trying to compare all permutations.. But one suggested solution was
beautiful and elegant (as in short) and boiled down to comparing the list to
its top-level reversal. It is true that anything with N identical copies
will be shown that way but only as a special case as it is a palindrome.
And, it begs the question of how python evaluates "list1 == list2" as that
may be just syntactic sugar for a method similar or different to the
straightforward ones.

I am surprised nobody suggested this one. First in English. Take the
original list. Assuming it is long enough, rotate it a notch so the former
first item is now at the end and the former second item is in front. Compare
the two versions of the list. A palindrome would be ruined by this simple
manipulation assuming you started with three or more. 

Before I present the trivial code, I want to remind everyone this is NOT a
suggested method. I do NOT want to get lectured at as if I suggested it. It
is what I call an academic exercise. It is a minor variant designed to foil
the darn palindrome case. For argument's sake, a list with no items or a
single item would be solved without calling the function below. Something
with two can harmlessly passed to the function but does not need to be. Only
three and above need apply. I have no doubt there are better ways but this
method would allow something small and nice like:

	a == sh(a)

Anyway, here is the code followed by the output just for ILLUSTRATION. If
you can show test cases where it fails, feel free. 

def shift_eq(listing):
    """Compare a list to a rotated version."""
    """Returning True if they are identical."""
    print("comparing:")
    print(listing)
    shifted = listing[1:] + listing[0:1]
    print(shifted)
    print("with Truth Value:")
    result = (listing == shifted)    
    print(result)
    return result

good, bad = [[1],[8]], [[666]]

shift_eq([ good, good, good, good, good ])
shift_eq([ good, bad, good, bad, good ])
shift_eq([ bad, good, good, good, good ])
shift_eq([ good, bad, good, good, good ])
shift_eq([ good, good, good, good, bad ])


Output
comparing:
[[[1], [8]], [[1], [8]], [[1], [8]], [[1], [8]], [[1], [8]]]
[[[1], [8]], [[1], [8]], [[1], [8]], [[1], [8]], [[1], [8]]]
with Truth Value:
True
comparing:
[[[1], [8]], [[666]], [[1], [8]], [[666]], [[1], [8]]]
[[[666]], [[1], [8]], [[666]], [[1], [8]], [[1], [8]]]
with Truth Value:
False
comparing:
[[[666]], [[1], [8]], [[1], [8]], [[1], [8]], [[1], [8]]]
[[[1], [8]], [[1], [8]], [[1], [8]], [[1], [8]], [[666]]]
with Truth Value:
False
comparing:
[[[1], [8]], [[666]], [[1], [8]], [[1], [8]], [[1], [8]]]
[[[666]], [[1], [8]], [[1], [8]], [[1], [8]], [[1], [8]]]
with Truth Value:
False
comparing:
[[[1], [8]], [[1], [8]], [[1], [8]], [[1], [8]], [[666]]]
[[[1], [8]], [[1], [8]], [[1], [8]], [[666]], [[1], [8]]]
with Truth Value:
False

Note if still reading, there would be a loop version of this in which you
zip together a list and the rotated version and compare each item in the
loop, leaving when any don't match.

Finally, on another forum for tutoring python that I no longer participate
in, I saw a request that may explain my own quandary. After reading the
request 9detail not that important) my first thought was how trivial it
might be to solve as it looked like they could use packages that would read
all the tabs in what looked like an EXCEL file into a list of DataFrame
objects that could then easily be manipulated to their needs. But their code
seemed to indicate they were trying to figure out how to do some of this
using more primitive methods by somehow getting the numbers into two lists.
There are oodles of tools to manipuIate a DataFrame and many more to
manipulate each numpy array composing it but I did not get involved. That
forum is for fairly new learners using base python and maybe the normally
bundled modules, not for numpy and pandas and the machine learning and
statistics modules I am focused on. On this forum, if someone asks for what
to use to get a job done rapidly and accurately, I might be happy to chime
in with what little I know.

-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On
Behalf Of Dennis Lee Bieber
Sent: Wednesday, January 9, 2019 2:02 PM
To: python-list at python.org
Subject: Re: How to find files with a string

On Wed, 9 Jan 2019 08:29:30 -0800 (PST), anton.gridushko at gmail.com declaimed
the following:

>Hello everyone!
>
>I need to find a file, that contains a string TeNum
>
	Well... The easiest way would be to use the OS "find" or "findstr"
command. You appear to be on Windows so:
...
<reverse("PINS")>




More information about the Python-list mailing list