Is it ok to do this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

27th Jan 2020, 1:12 PM
Easham Halder
Easham Halder - avatar
5 Answers
+ 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?
27th Jan 2020, 1:17 PM
HonFu
HonFu - avatar
+ 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:
30th Jan 2020, 5:58 PM
Anand Sharma
Anand Sharma - avatar
+ 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.
31st Jan 2020, 6:28 AM
Rick Shiffman
Rick Shiffman - avatar
+ 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.
30th Jan 2020, 5:25 PM
SIDDHANI VAMSI SAI KUMAR🇮🇳
SIDDHANI VAMSI SAI KUMAR🇮🇳 - avatar
+ 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.
30th Jan 2020, 11:35 PM
GeraltdeRivia