Doctor Delta
Doctor Delta

Reputation: 3

Python 3- "local variable referenced before assignment" with imported code

I am working on a school assignment that requires me to use the following code:

#!/usr/bin/python3

import re
jfif = re.compile(b'\xff\xd8\xff\xe0..JFIF\x00',
              flags = re.DOTALL)
exif = re.compile(b'\xff\xd8\xff\xe1..EXIF\x00',
              flags = re.DOTALL)
spiff = re.compile(b'\xff\xd8\xff\xe8..SPIFF\x00',
              flags = re.DOTALL)

f = open('manson.dd', 'rb')
sector = f.read(512)

count = 0

while len(sector) != 0:
     if jfif.match(sector):
         count += 1
         print('found JFIF at offset {:X}, sector {}'.format(
             f.tell()-512, (f.tell()  - 512) // 512))

     elif exif.match(sector):
         count += 1
         print('found EXIF at offset {:X}, sector {}'.format(
             f.tell()-512, (f.tell()  - 512) // 512))

     elif pdf.match(sector):
         pdfCount += 1
         print('found PDF at offset {:X}, sector {}'.format(
             f.tell()-512, (f.tell()  - 512) // 512))
     sector = f.read(512)

print("Found", count, "JPEG Files")

The assignment requires me to make a new script that finds GIF files and imports the actual search function into a script that prompts for the file and prints the result

This is what I have so far

Main Script

#!/usr/bin/python3

import search

fileName = input("Enter the file you would like to search: ")
f = open(fileName, 'rb')
sector = f.read(512)

search.crawl()

print("Found", count, "GIF Files")

Imported Code

#!/usr/bin/python3
import re

gif = re.compile(b'\x47\x49\x46\x38..GIF\x61',
              flags = re.DOTALL)
def crawl():
    count = 0
    while len(sector) != 0:
             if gif.match(sector):
                 count += 1
                 print('found GIF at offset {:X}, sector {}'.format(
                     f.tell()-512, (f.tell()  - 512) // 512))
                 sector = f.read(512) 

When I run the main script, I am told that "the local variable 'sector' is referenced before assignment'. I haven't the slightest idea how to rectify this.

Upvotes: 0

Views: 716

Answers (2)

stevetu21
stevetu21

Reputation: 158

Instead of def crawl(), use def crawl(sector). And then instead of calling search.crawl(), call search.crawl(sector).

Also, you will encounter a similar error with count. So you should modify search.crawl(sector) to return count and then when you call search.crawl(sector) in main(), assign to variable count.

In main:

count = search.crawl(sector)

In search:

def crawl(sector):
    count = 0
    while len(sector) != 0:
             if gif.match(sector):
                 count += 1
                 print('found GIF at offset {:X}, sector {}'.format(
                     f.tell()-512, (f.tell()  - 512) // 512))
                 sector = f.read(512) 
    return count

The reason for this is because when you import the module search, it has no notion of the sector variable. The only way for main's sector to be sent into search.crawl() is for it to be passed in as a function parameter a la search.crawl(sector).

Upvotes: 1

Angel Cruijff
Angel Cruijff

Reputation: 158

sector variable isn't global, you can pass it to crawl function

def crawl(sector):

and then:

search.crawl(sector)

Upvotes: 1

Related Questions