os.system doesn#39;t work in Python(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>
执行后,一定是这样的:
After execution, must be this:
C:>
请帮忙.
推荐答案
首先,你一般不想使用 os.system - 看看 子进程模块.但是,这并不能解决您的直接问题(只是您可能遇到的一些问题)- cd 不起作用的实际原因是因为它更改了 subprocess<的工作目录/em>,并且不会影响 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 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:os.system 在 Python 中不起作用
基础教程推荐
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 包装空间模型 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
