os.system在Python中不起作用 [英] os.system doesn't work in Python

查看:3469
本文介绍了os.system在Python中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用windows vista,但我从DOS命令运行python。我有这个简单的Python程序。 (它实际上是一个名为test.py的py文件)

I'm working on windows vista, but I'm running python from DOS command. I have this simple python program. (It's actually one py file named test.py)

import os
os.system('cd ..')



当我从Dos命令执行python test.py时,它不工作。
例如,如果执行前的提示Dos Command为:

When I execute "python test.py" from a Dos command, it doesn't work. For example, if the prompt Dos Command before execution was this:

C:\Directory>

执行后,必须是:

C:\>

帮助Plz。

推荐答案

首先,你通常不想使用 os.system - 请查看子处理模块。但是,这不会解决你的直接问题(只是一些你可能有轨道) - 实际原因 cd 将无法工作是因为它改变工作目录 ,并且不影响Python正在运行的进程 - 要执行此操作,请使用 os.chdir

First, you generally don't want to use os.system - take a look at the subprocess module instead. But, that won't solve your immediate problem (just some you might have down the track) - the actual reason cd won't work is because it changes the working directory of the subprocess, and doesn't affect the process Python is running in - to do that, use os.chdir.

这篇关于os.system在Python中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆