pedastrian57's blog

By pedastrian57, history, 4 years ago, In English

Today I am trying to find something similar to #ifndef ONLINE_JUDGE in python . I find a post on stackoverflow .This has worked for me . You can insert the below code in your program to do conditional compilation ....

import sys
zz=not __debug__
if not zz:
	input=sys.stdin.readline
else:	
	sys.stdin=open('input.txt', 'r')
	sys.stdout=open('output.txt','w')
  • Vote: I like it
  • +9
  • Vote: I do not like it

»
4 years ago, # |
Rev. 3   Vote: I like it +11 Vote: I do not like it

You can define your environment variable, and access in code like:

import os

if os.environ.get("your_env_name"):
    ...
else:
    ...
»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it

another way

import sys
from os import path
if(path.exists('input.txt')):
    sys.stdin = open("input.txt","r")
    sys.stdout = open("output.txt","w")
 
»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

OP is 4 yeas old by now, but in case anyone is about to use it, please know that it does work on 29 March, 2024.