Finding all possible paths between two points in a grid using recursion I'm trying to find all possible paths made of 0 from bottom left to top right, only by moving either up or to the right. The input file contains a grid of characters, where 1 represents an obstacle and 0 represents a vacant cell.

The problem I have is the code not finishing/ taking too long. The code works correctly with smaller inputs like this, but on a large file with 5000 rows and 5000 characters in each row it never finishes even after 20 minutes.

import sys
sys.setrecursionlimit(900000000)

file_e = open('file.txt','r')
file = file_e.readlines()
file_e.close()
file.reverse()
for index,line in enumerate(file):
    file[index] = line.split(' ')
print(len(file)-1, len(file[0])-1)
def end(file,y,x):

    if y == len(file)-1 and x == len(file[0])-1: return 1

    try:
        if file[y+1][x] == '0': up = True
    except: pass
    try:
        if file[y][x+1] == '0': right = True
    except: pass

    try:
        if up == True and right == True: return end(file,y+1,x) + end(file,y,x+1) 
    except: pass


    try:
        if up == True: return end(file,y+1,x) 
    except: pass

    try:
        if right == True: return end(file,y,x+1) 
    except: pass


    if file[y+1][x] == '1' or file[y][x+1] == '1':
        return 0



print(end(file,0,0))
No optimization will make this run effectively for large inputs because fundamentally the "all paths problem" is hard. In short, as the size of the input increases incrementally, the number of paths increases exponentially. Your best option may be to ask "what is a similar question I can ask that will give me a useful answer for less computational expense."

In terms of code quality, using try-catch structures for branching is often considered bad form. In many languages this would be a performance issue, but from my 30 seconds of googling it appears the performance impact isn't huge in python. 

One adjustment you could make is to parse the file into an adjacency matrix and then using a known algorithm to solve for your lists of paths.