Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line execution in different folder

I'm calling a command line program in python using the os.system(command) call.

How can I call this command passing a different folder for execution? There is a system call for this? Or I should save the current folder, and, after execution, change restore it.

like image 854
Victor Avatar asked Dec 06 '12 13:12

Victor


People also ask

How do I direct command prompt to a specific folder?

What to Know. Type cmd into the search bar to open the command prompt. Shift + right click in a window, then click Open PowerShell Window here to access the PowerShell interface. Open the folder you wish to access, then type cmd into the folder path at the top of the window to open a command prompt within the folder.

How do I change my cmd directory?

Here are the different ways to change your CMD directory in Windows 10 or Windows 11: Open the Command prompt, type in cdcdThe cd command, also known as chdir (change directory), is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.https://en.wikipedia.org › wiki › Cd_(command)Cd (command) - Wikipedia followed by the directory you want to move into, and hit Enter. Change the Windows directory by entering the drive name followed by : in the Command prompt, and hit Enter.

How do I change C directory to D in cmd?

For instance, if you wanted to change the drive from C: to D:, you should type: d: … and then press Enter on your keyboard. To change the drive and the directory at the same time, use the cd command, followed by the /d switch.

How do I run a command prompt from a different drive?

If you want to open elevated Command Prompt, you should press Ctrl + Shift + Enter. After the Command Prompt opens, you can type the drive letter of the desired drive, followed by a colon, e.g. C:, D:, and hit Enter. The CMD.exe will change to the drive letter of the target drive.


1 Answers

The subprocess module is a very good solution.

import subprocess p = subprocess.Popen([command, argument1,...], cwd=working_directory) p.wait() 

It has also arguments for modifying environment variables, redirecting input/output to the calling program, etc.

like image 74
hynekcer Avatar answered Sep 28 '22 02:09

hynekcer