import linecache import sys import os sys.path = ['.'] + sys.path + ['foo'] os.system('mkdir -p foo/bar') os.system('echo "correct line" > foo/bar/test.txt') os.system('echo "wrong line!" > test.txt') print linecache.getline('bar/test.txt', 1)