here is the code i am trying to read ascii value which is string and print , but execution stops at file open file print statement :(

import os
import sys
import string

filename = "c:\\test.exe"
try:
    f = open(filename,"rb")
except IOError:
    print'cannot open the file'
    sys.exit(0)


print'file is opened'



try:
    s=""
    a=f.read(1)
    while((a.isalnum() )or( a.isspace())):
        s=s+a
        a=f.read()
        
    iLen = len(s)
    if(iLen>3):
        print(s)
        

except EOFError:
    sys.exit(0)

Recommended Answers

All 5 Replies

Where does execution stop? Does it give an error? Where is this so-called "file open file print" statement?

execution stops with

print'file is opened'

it does not print , the string from file

Note that you are reading an "exe" file. The first character is probably not alphanumeric or a space so it stops. You haven't gotten many responses because of laziness. Add a print statement to see what is being read [probably want to print ord(a) since it is not a text file], and print a message at critical points in the program so you know what is going on. Especially when you are entering a try or except block. If you get an error other than IO or EOF the except block won't print. Also, if the length of s is less than 4, you won't get any output so add an else: "length of s is too small", len(s). As I said, this type of post doesn't get much of a response because you haven't done any debugging yourself.

i did tried to break and check , but it failed to break. will try to give print and check @ code.thanks for your feedback woooee

You definitely want to add a print statement here

try:
    s=""
    a=f.read(1)
    while((a.isalnum() )or( a.isspace())):
        s=s+a
        a=f.read()
        print "'a' is now", a
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.