os.system问题 [英] os.system question

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

问题描述

大家好,


我将Perl脚本移植到Python脚本。一切都会被罚款,直到

调用os.system()。


在我的脚本中,将执行许多DOS命令。

表示new_folder,old_folder位于folder_array:

os.system(''MD"''+ new_folder +''"'');

os。 system(''XCOPY"''+ old_folder +''""''+ new_folder +''"'');


在Perl中,所有输出都将是直接打印在控制台中。

但在Python中,输出将打印在单独的cmd窗口中。


是否可以防止这么多cmd-windows到打开并让所有

输出直接打印在Python shell中?

最好的问候^^)

-

___

oo // \\

(_,\ / \_ / \ Xu,Qian

\\ \\ \\ \\ _ / _ \_ / stanleyxu2005

/ _ / \_ \

解决方案

12月28日下午12:57,stanleyxu& lt; no_re ... @ microsoft.comwrote:


注意在IDLE编辑器中调试脚本时会出现此问题。

当我加倍时点击my_script.py,所有输出将打印在一个

控制台上。


-

___

oo // \\

(_,\ / \_ / \ Xu,Qian

\ \ _ / _ \\ \\ _ / stanleyxu2005

/ _ / \ _



为什么你首先使用os.system来执行这些命令?你应该使用os和shutil模块,因为它们会更加平台友好。


类似于这个:


#未经测试

for new_folder,old_folder in folder_array:

os.mkdir(new_folder)

shutil.copytree(old_folder,new_folder)

根据需要在mkdir调用中调整路径。


有关详细信息,请参阅shutil的文档:
http://docs.python.org/lib /module-shutil.html


这里有一些文件夹操作文档:
http://effbot.org/librarybook/os.htm


by by,the应该使用子进程模块代替

os.system和os.popen *调用: htt p://docs.python.org/lib/module-subprocess.html

Mike


< a href =mailto:ky ****** @ gmail.com> ky ****** @ gmail.com 写道:


12月28日下午12:57,stanleyxu< no_re ... @ microsoft.comwrote:


>注意在调试脚本时出现此问题IDLE编辑器。
当我双击my_script.py时,所有输出都将打印在一个
控制台中。

-
___
oo / / \\
(_,\ / \_ / \ Xu,Qian
\ \ _ / _ \_ / stanleyxu2005
/ _ / \_




为什么你首先在这些命令中使用os.system?你应该使用os和shutil模块,因为它们会更加平台友好。


类似于这个:


#未经测试

for new_folder,old_folder in folder_array:

os.mkdir(new_folder)

shutil.copytree(old_folder,new_folder)


根据需要在mkdir调用中调整路径。


请参阅shutil'的docs获取更多信息:
http://docs.python .org / lib / module-shutil.html


这里有一些文件夹操作文档:
http://effbot.org/librarybook/os.htm


By by,子进程模块应该用来代替

os.system和os.popen *调用: http://docs.python.org/lib/module-subprocess.html

Mike



谢谢Mike,


你提供了另一种选择。


但我的问题还没有得到解答。我之所以使用

os.system(),是因为我希望通过写一个

脚本来避免意外删除文件。我的真实脚本如下:


#1。在DOS控制台执行命令的功能

def execCommand(cmd):

如果DEBUG_MODE:

打印''DOS''+ cmd;

else:

os.system(cmd);


#2.1创建临时文件夹。首先删除它,如果它存在。

如果os.path.exists(tmp_folder):

execCommand(''RD"''+ tmp_folder +''" / S / Q'');

execCommand(''MD"''+ tmp_folder +''"'');


#2.2将所有文件复制到临时文件夹,这些文件将放在

包中。

为source_folder,dest_folder在folders_array中:

如果没有os.path.exists(dest_folder):

execCommand(''MD"''+ dest_folder +''"'');

execCommand('''XCOPY \"''+ source_folder +''""''+ dest_folder +''" / Y'');

好​​处是,当我设置DEBUG_MODE = True时,我可以看到将执行什么
。这样我就可以确保我的脚本不会意外删除任何其他重要文件。


-

___

oo // \\

(_,\ / \_ / \ Xu,Qian

\ \ _ / _ \_ / stanleyxu2005

/ _ / \_ \


12月28日下午1:52,stanleyxu< ; no_re ... @ microsoft.comwrote:


大家好,


我将Perl脚本移植到Python脚本。一切都会被罚款,直到

调用os.system()。


在我的脚本中,将执行许多DOS命令。

表示new_folder,old_folder位于folder_array:

os.system(''MD"''+ new_folder +''"'');

os。 system(''XCOPY"''+ old_folder +''""''+ new_folder +''"'');


在Perl中,所有输出都将是打印在控制台目录中等等。

但在Python中,输出将打印在单独的cmd窗口中。


是否可以防止打开如此多的cmd窗口让所有

输出直接打印在Python shell中?



请考虑使用子流程模块。它有比os.system更多的选项

,包括I / O重定向,这似乎是你需要的价格。


在IDLE中,你必须捕获程序的输出并自行打印

,因为你不能(AFAIK)在IDLE中运行DOS shell

窗口。未经测试:

导入子流程


output = subprocess.Popen(''MD"''+ new_folder +''"'',shell = True ,

stdout = subprocess.PIPE,stderr = subprocess.STDOUT).communicate()[0]

打印输出

Carl Banks


Hi All,

I am porting Perl script to Python script. Everything works fines until
calling os.system().

In my script, a number of DOS-commands will be executed.
for new_folder, old_folder in folder_array:
os.system(''MD "'' + new_folder + ''"'');
os.system(''XCOPY "'' + old_folder + ''" "'' + new_folder + ''"'');

In Perl, all outputs will be printed in console directly.
But in Python, outputs will be printed in separated cmd-windows.

Is it possible to prevent so many cmd-windows to be opened and let all
output be printed direct in Python shell?
best regards ^^)
--
___
oo // \\
(_,\/ \_/ \ Xu, Qian
\ \_/_\_/ stanleyxu2005
/_/ \_\

解决方案

On Dec 28, 12:57 pm, stanleyxu <no_re...@microsoft.comwrote:

To note this problem occurs when debugging script in IDLE editor.
When I double click on my_script.py, all outputs will be printed in one
console.

--
___
oo // \\
(_,\/ \_/ \ Xu, Qian
\ \_/_\_/ stanleyxu2005
/_/ \_


Why are you using os.system for these commands in the first place? You
should be using the os and shutil modules instead as they would be
more cross-platform friendly.

Something like this:

# untested
for new_folder, old_folder in folder_array:
os.mkdir(new_folder)
shutil.copytree(old_folder, new_folder)
Adjust the path as needed in the mkdir call.

See shutil''s docs for more info:
http://docs.python.org/lib/module-shutil.html

And here''s some folder manipulation docs:
http://effbot.org/librarybook/os.htm

By the by, the subprocess module is supposed to be used in place of
the os.system and os.popen* calls: http://docs.python.org/lib/module-subprocess.html

Mike


ky******@gmail.com wrote:

On Dec 28, 12:57 pm, stanleyxu <no_re...@microsoft.comwrote:

>To note this problem occurs when debugging script in IDLE editor.
When I double click on my_script.py, all outputs will be printed in one
console.

--
___
oo // \\
(_,\/ \_/ \ Xu, Qian
\ \_/_\_/ stanleyxu2005
/_/ \_



Why are you using os.system for these commands in the first place? You
should be using the os and shutil modules instead as they would be
more cross-platform friendly.

Something like this:

# untested
for new_folder, old_folder in folder_array:
os.mkdir(new_folder)
shutil.copytree(old_folder, new_folder)
Adjust the path as needed in the mkdir call.

See shutil''s docs for more info:
http://docs.python.org/lib/module-shutil.html

And here''s some folder manipulation docs:
http://effbot.org/librarybook/os.htm

By the by, the subprocess module is supposed to be used in place of
the os.system and os.popen* calls: http://docs.python.org/lib/module-subprocess.html

Mike

Thanks Mike,

you have provided another option.

But my question has not been answered yet. The reason, why I use
os.system(), is that I want to avoid accident file deletion by writing a
script. My real script looks like:

# 1. Funtion to execute a command in DOS-console
def execCommand(cmd):
if DEBUG_MODE:
print ''DOS'' + cmd;
else:
os.system(cmd);

# 2.1 Creates temp folder. Removes it first, if it exists.
if os.path.exists(tmp_folder):
execCommand(''RD "'' + tmp_folder + ''" /S /Q'');
execCommand(''MD "'' + tmp_folder + ''"'');

# 2.2 Copies all files to the temp folder, that are going to be put in
package.
for source_folder, dest_folder in folders_array:
if not os.path.exists(dest_folder):
execCommand(''MD "'' + dest_folder + ''"'');
execCommand(''XCOPY \"'' + source_folder + ''" "'' + dest_folder + ''" /Y'');
The benefit is that, when I set DEBUG_MODE=True, I can see what will be
executed. So that I can make sure that my script will not delete any
other important files by accident.

--
___
oo // \\
(_,\/ \_/ \ Xu, Qian
\ \_/_\_/ stanleyxu2005
/_/ \_\


On Dec 28, 1:52 pm, stanleyxu <no_re...@microsoft.comwrote:

Hi All,

I am porting Perl script to Python script. Everything works fines until
calling os.system().

In my script, a number of DOS-commands will be executed.
for new_folder, old_folder in folder_array:
os.system(''MD "'' + new_folder + ''"'');
os.system(''XCOPY "'' + old_folder + ''" "'' + new_folder + ''"'');

In Perl, all outputs will be printed in console directly.
But in Python, outputs will be printed in separated cmd-windows.

Is it possible to prevent so many cmd-windows to be opened and let all
output be printed direct in Python shell?


Consider using the subprocess module instead. It has more options
available than os.system, including I/O redirection, which seems to be
what you need.

In IDLE, you''ll have to capture the output of the programs and print
it yourself, since you can''t (AFAIK) run a DOS shell in an IDLE
window. Untested:
import subprocess

output = subprocess.Popen(''MD "'' + new_folder + ''"'', shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0]
print output
Carl Banks


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

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