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 中不起作用


- 我如何透明地重定向一个Python导入? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01