Do open() and read() in python execute the file?

Viewed 658

If I open a binary or text file in Python with the open and read() commands to be written somewhere else, will it be executed? Could this expose the computer to malware if the file contains malicious code?

1 Answers

No, read and open file does not execute the executable file in python. If you want you can read the executable file in binary by adding "b"

open("readfile.txt", "rb")

For executing the py file you need to use "exec" function like

execfile('hello.py')

For executing external exe file there are several ways,where you need to import "os" or "subprocess".

example you can execute file by using the below function.

import os
os.startfile("C:\Documents and Settings\flow_model\flow.exe")

Related