I know there are available options like os and subprocess but they don't just do what I want yet
Let's say I have a list of external commands.myList = ['cd desktop','mkdir spotify']
and I want to run them all at once from python, I don't want to use os.chdir or any sub process method because that list is based up of user input and I can't just know in what index of the list they have to cd into a project, please help
Asked
Active
Viewed 81 times
0

Pleasanttech
- 1
- 1
-
Does this answer your question? [What is the return value of os.system() in Python?](https://stackoverflow.com/questions/6466711/what-is-the-return-value-of-os-system-in-python) – sushanth Dec 29 '20 at 09:49
-
that is about the return value of `os.system` and the user is worried about the cd commands would not work – AmaanK Dec 29 '20 at 09:51
-
It doesn't, when I add cd into os.system() it doesn't work for some technical reason, if there was a way I could pass cd into it as an argument and it'd work. – Pleasanttech Dec 29 '20 at 09:52
-
Have you considered using `subprocess` and supplying the working dir via the `cwd` command? See https://docs.python.org/3/library/subprocess.html#using-the-subprocess-module – yvesonline Dec 29 '20 at 10:04
-
1`os.system()` is deprecated, the `subprocess` module should be used to run executables __without__ using `cmd.exe`. Python is a __very__ powerful script interpreter. It is much more powerful than Windows command processor `cmd.exe`. Creating a directory can be done with Python code. The usage of `cmd.exe` to create a directory is really nonsense. Writing a Python script which uses one of the most powerful script interpreters to run commands with one of the oldest and least powerful interpreters is clearly the wrong approach for the task to be done by the Python script. – Mofi Dec 29 '20 at 18:14
-
1I am not sure what should be the benefit for a user using the Python script written by you in comparison to opening a [command prompt](https://www.howtogeek.com/235101/) and running the commands directly with `cmd.exe` without the need to install Python and your Python script. – Mofi Dec 29 '20 at 18:17
1 Answers
0
So, the basic worry comes is about the cd
command. for which we can make exceptional case by simply spliting and checking if the command is cd which will detect weather the command is cd
or not.
So we will start of by making a function that will check our command
import os
import shlex
def is_cd(command: str) -> bool:
command_split = shlex.split(command)
return command_split[0] == "cd" # this returns True if command is cd or False if not
now we can use our above function to identify if the command is cd or not. Then we need to execute the command for which we will use the function below
def run_command(command: str) -> int:
if is_cd(command):
split_command = shlex.split(command)
directory_to_change = ' '.join(split_command[1:])
os.chdir(directory_to_change)
else: # if its a regular command
os.system(command)
so our final code becomes
import os
import shlex
def is_cd(command: str) -> bool:
command_split = shlex.split(command)
return command_split[0] == "cd" # this returns True if command is cd or False if not
def run_command(command: str) -> int:
if is_cd(command):
split_command = shlex.split(command)
directory_to_change = ' '.join(split_command[1:])
os.chdir(directory_to_change)
else: # if its a regular command
return_code = os.system(command)
if return_code != 0: # If command failed to run then
pass # you can do something here
if __name__ == "__main__":
user_commands = ["mkdir testdir", "cd testdir"]
for command in user_commands:
run_command(command)
Here are some extra links to understand the concepts: