I'll-Be-Back
I'll-Be-Back

Reputation: 10828

Return Index Position

Coming from PHP background, I am learning how to use Python.

I am trying to return Index Position if content are found in the haystack from needle

haystack= open("haystack.wav",'r')
needle = open("needle.wav",'r')
nedOffset = needle.read()[:46]

print(haystack.index(nedOffset))

It don't seem to work, I get an error:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    print(haystack.index(ned[:46]))
AttributeError: '_io.TextIOWrapper' object has no attribute 'index'

How to fix?

In PHP I would do:

$needle = file_get_contents("needle.wav", false, null, 46);
$haystack = file_get_contents("heystack.wav");
echo strpos($haystack,$needle);

Upvotes: 0

Views: 166

Answers (1)

David Robinson
David Robinson

Reputation: 78610

You need to read in the string from haystack, not just needle. Something like:

haystack= open("haystack.wav",'r').read()

or

with open("haystack.wav",'r') as inf:
    haystack = inf.read()

Upvotes: 2

Related Questions