python - Loop doesn't process other files in the directory -


me=1  while (me < 244):     f=open('%s' % me, 'rb')      tdata = f.read()     f.close()      ss = '\xff\xd8'     se = '\xff\xd9'      count = 0     start = 0     while true:         x1 = tdata.find(ss,start)         if x1 < 0:             break         x2 = tdata.find(se,x1)         jpg = tdata[x1:x2+1]         count += 1         fname = 'extracted%d03.jpg' % (count)         fw = open(fname,'wb')         fw.write(jpg)         fw.close()         start = x2+2         me=me+1 

i trying run multiple files. operation file 1 , rest of files ignored. new python can tweak bit?

in last line of code you're incrementing me inside of nested while loop want run each of files. fix it, un-indent me so.

#!/usr/bin/python  me=1  while (me < 244):     f=open('%s' % me, 'rb')      tdata = f.read()     f.close()      ss = '\xff\xd8'     se = '\xff\xd9'      count = 0     start = 0     while true:         x1 = tdata.find(ss,start)         if x1 < 0:             break         x2 = tdata.find(se,x1)         jpg = tdata[x1:x2+1]         count += 1         fname = 'extracted%d03.jpg' % (count)         fw = open(fname,'wb')         fw.write(jpg)         fw.close()         start = x2+2      me=me+1  # needs outside of nested while loop 

that being said, want improve names of variables in code (make them more descriptive!), , idea extract code in while loop function. it's worth mentioning outer while loop can (and should be) replaced loop.

something this:

def do_something_with_file(me):     f=open('%s' % me, 'rb')      tdata = f.read()     f.close()      ss = '\xff\xd8'     se = '\xff\xd9'      count = 0     start = 0     while true:         x1 = tdata.find(ss,start)         if x1 < 0:             break         x2 = tdata.find(se,x1)         jpg = tdata[x1:x2+1]         count += 1         fname = 'extracted%d03.jpg' % (count)         fw = open(fname,'wb')         fw.write(jpg)         fw.close()         start = x2+2   in range(1, 244):     do_something_with_file(i) 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -