Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 577
Jlahamz Avatar asked May 22 '26 08:05

Jlahamz


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")

like image 131
ARVIND RAJ Avatar answered May 23 '26 20:05

ARVIND RAJ