+ 9
Is it ok to do this?
Is it ok to use this code Is this code dangerous https://code.sololearn.com/cOUoIhEUEhte/?ref=app
6 Réponses
+ 6
If you can pass strings to the system function and execute them, you can also loop over a list of strings.
Probably best to be careful with os operations though and explicitly write them - you can, depending on what you do - do quite a lot of damage, right?
+ 2
Yes, it is okay to do that in any environment, because you might face sometimes where you have to execute shell commands using your Python program and this will be the way to do that.
Remember, use this command with caution as if not coded correctly hackers may find a way to crack into your system. Some of the examples of it are to take command from the input and running it directly over os.system.
Otherwise, it is totally safe to do so.
P.S.: Sololearn's Server prohibits "mkdir" or "rm -r *" commands because after all they know that this place is full of script kiddies :lol:
+ 2
Hi there Easham, I think that example of using the OS from python is safe. ls commands are harmless and you are not running as "root" or
the superuser on the system. The worst you can do is mess up your own processes. Again the commands you used are harmless.
+ 1
Of course. ls command lists directory contents. it doesn't do any harm. In addition to this, as @HonFu mentioned, it is better to be careful with os operations.
+ 1
To run commands I think that is better to use subprocess instead of the os module.
import subprocess
subprocess.run(['ls', '-l', 'usercode'])
However, if that would go to real world application, yes, it would require some validation to prevent that a malicious user inserts commands that might make harm. But, as others said i don't see any problem in running a command to access to the system commands, however I think that is better using the subprocess module.