简体   繁体   中英

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?

remove the "." at the start of the command. By typing "./home/..." your are asking to 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 also might have to start the command by "python". So your final command will be
python /home/user/code/update.py

to correct it:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM