Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6346863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:06:02+00:00 2026-05-24T21:06:02+00:00

I am a newer to python. After I leraned some basic in python, I

  • 0

I am a newer to python. After I leraned some basic in python, I try to write some program by imitating other’s.
And I find some code in python2.0 and it can’t work in 3.0.
How can I fix it.

import sys
import os
import string

headers = [ ('JFIF', 6, 'jpg'), ('GIF', 0, 'gif'), ('PNG', 1, 'png') ] 
marker = []
fileName = r'd:\\first.doc'

try:
    fid = open(fileName, 'rb')   #open file in binary mode not text mode
except:
    print("can't open file",fileName)
    sys.exit(1)

s = 0
for line in fid:
    for flag, offset, ext in headers:
        index = string.find(line, flag)  #error occurs here. 
        if index > 0 :
            pos = s + index - offset
            marker.append((pos, ext))
        s += len(line)

————————after edit————————————-
The purpose of this code is to save pictures in the documents like doc,pdf.
And this is the first step, which is to find the pictures’ header in the file

I would try the following correction,but failed
index = string.find(line, flag) -> index = line.find(flag)

index = string.find(line, flag) -> index = str.find(line,flag)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:06:03+00:00Added an answer on May 24, 2026 at 9:06 pm

    change

    headers = [ ('JFIF', 6, 'jpg'), ('GIF', 0, 'gif'), ('PNG', 1, 'png') ] 
    ...
    string.find(line, flag)
    

    to

    headers = [ (b'JFIF', 6, 'jpg'), (b'GIF', 0, 'gif'), (b'PNG', 1, 'png') ] 
    ...
    line.find(flag)
    

    the string. change is really just moving the call to the object rather than using a library.

    the b'' change is more interesting. python 3 is more careful with the difference between strings and bytes. because you opened your file in binary mode it is returning bytes. so you need to check what is returned against bytes, rather than against strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't find newer Microsoft Blend Preview. Is it available?
Suppose I have some Python code like the following: input = open(input.txt) x =
Why isn't this simple Python code working? import urllib file = urllib.urlopen('http://www.google.com') print file.read()
So I wrote a Python script which does some simple stuff. It was originally
This code used to work on Vista (and Windows XP) but after an upgrade
I'm trying to write a simple Python script for my mobile phone to periodically
I am using python 2.6.6 I am simply trying to restart the program based
My python program has many function which all seem to work fine when run
I often feel that after iterating over my code for a number of times
I recently switched OS and am using a newer Python (2.7). On my old

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.