I have a program that's basically as follows:
for l in range(0,100): file = open("C:/Twitter/json/user_" + str(l) + ".json", "r") #do some stuff file.close()
I am trying to figure out a way to handle the exception that will be thrown if say file 20 is missing, and tell it to
continue. I attempted to use the
continue con un
try statement however, it kept complaining that I wasn't putting it in the loop properly. Any advice would be appreciated.
Basically I tried:
try: for: except: continue
preguntado el 09 de enero de 11 a las 12:01
import json for l in xrange(100): try: with open('C:/Twitter/json/user_%d.json' % l, 'r') as f: data = json.load(f) #do stuff with obj except IOError: pass
editar fixed the code.
you could check file existence and then open it:
import os.path os.path.exists(file_path)