How to find position of word in file?

前端 未结 3 2017
太阳男子
太阳男子 2021-01-02 21:32

for example I have file and word \"test\". file is partially binary but have string \"test\". How to find position of word ( index ) in file without load to memory this file

3条回答
  •  难免孤独
    2021-01-02 22:16

    You cannot find the position of a text within a file unless you open the file. It is like asking someone to read a newspaper without opening the eye.

    To answer the first part of your question, it is relatively simple.

    with open('Path/to/file', 'r') as f:
        content = f.read()
        print content.index('test')
    

提交回复
热议问题