You need to import the variables from the file. You can import all of them like this:
from variables import *
if(flag == 0) :
j = j+1
Or reference a variable from the imported module like this variables.flag
import variables
if(variables.flag == 0) :
j = j+1
Or import them one by one like this
from variables import flag, j
if(flag == 0) :
j = j+1
The best way is to use variables.flag preserving the namespace variables because when your code grows large, you can always know that the flag variable is coming from the module variables. This also will enable you to use the same variable name flag within other modules like module2.flag