Hami
Hami

Reputation: 117

How to run python script in command line

I want to run a python script which is located in "path=/home/user/code" in another python code by os.system, in linux. I run the following code and it runs well:

os.system("cd "+path+ ";" + "./update.py")

But I want to run the script without changing the current directory. So when I run the following code:

os.system("."+path + "/update.py")

I get an error that says: "./home/user/code/update.py not found"

How can I solve it?

Upvotes: 0

Views: 759

Answers (3)

MDEV
MDEV

Reputation: 938

If you want to run Python script in windows using CMD. Just use py to run it

py myfile.py

or

python myfile.py

It works very well in Windows CMD/PowerShell.

NOTE: make sure to run cmd on that directory where the Python script is available. Otherwise, you have to put a complete path.

Upvotes: 0

Chili
Chili

Reputation: 154

Remove the "." at the start of the command.

By typing "./home/..." you are asking Python to search for a folder called "home" in the current directory. If you remove the ".", the path will be interpreted as absolute and it should work. You might also have to provide the python keyword at the start of your command. So your final command will be:

python /home/user/code/update.py

Upvotes: 1

Tal Folkman
Tal Folkman

Reputation: 2581

to correct it:

os.system(path + "/update.py")

so basically it will be like - os.system(/home/user/code/update.py)

Upvotes: 0

Related Questions