How to use 'runfile python' in Python

Every line of 'runfile python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
18def run_file(path):
19 with open(path) as f:
20 execute(f.read())
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

389def _run():
390 global __file__
391 import os
392 import site # noqa: F401
393 sys.frozen = 'macosx_app'
394
395 argv0 = os.path.basename(os.environ['ARGVZERO'])
396 script = SCRIPT_MAP.get(argv0, DEFAULT_SCRIPT) # noqa: F821
397
398 sys.argv[0] = __file__ = script
399 if sys.version_info[0] == 2:
400 with open(script, 'rU') as fp:
401 source = fp.read() + "\n"
402 else:
403 with open(script, 'rb') as fp:
404 encoding = guess_encoding(fp)
405
406 with open(script, 'r', encoding=encoding) as fp:
407 source = fp.read() + '\n'
408
409 BOM = b'\xef\xbb\xbf'.decode('utf-8')
410
411 if source.startswith(BOM):
412 source = source[1:]
413
414 exec(compile(source, script, 'exec'), globals(), globals())

Related snippets