os.system的问题

atlantis141 2011-04-01 02:15:41
在跟着简单python初学.现在碰到一个问题,是做一个压缩处理的过程
环境变量 winrar C:\Program Files\WinRAR\WinRAR.exe;%SystemRoot%\system32
我在系统运行:winrar a C:\a1.rar C:\Download C:\Qt 成功说明环境变量是对的

但是我在写的
source = r'C:\Qt'
target_dir = 'C:\\'
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.rar'
zip_command = "winrar a '%s' %s" % (target, source)
#zip_command = "cmd"
if os.system(zip_command) == 0:
print 'Successful backup to', target
else:
print 'Backup FAILED'


'winrar' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

如果我把zip_command = "cmd" 倒是可以运行的 这个何解

...全文
1733 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
cheungxue 2011-04-02
  • 打赏
  • 举报
回复
回帖有分拿吗?
iambic 2011-04-02
  • 打赏
  • 举报
回复
解决方法就是不用os.system。os.system根本不值得学习。学会了只会助长驾御过时技术信心。
如果一个人有好代码不写,非要写破代码,写了破代码发现运行不起来,非要把不能运行的破代码修成可以运行的破代码。这种情况,是不宜助纣为虐的。
atlantis141 2011-04-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 iambic 的回复:]
你若用subprocess根本不会有这种事情。自找麻烦,自己解决。不如当初就别制造麻烦。
[/Quote]
用哪个好不是这个帖子讨论的重点,我看了你给我的提示我也知道subprocess比较好用,我也用了.
但是我要的是解决这个问题 os.system为什么一开始不能运行.我希望学习一下os.system的用法
讨论的是如何解决不是哪个好.
iambic 2011-04-02
  • 打赏
  • 举报
回复
你若用subprocess根本不会有这种事情。自找麻烦,自己解决。不如当初就别制造麻烦。
aleng18 2011-04-02
  • 打赏
  • 举报
回复
os.system根本不值得学习-------你有能耐让python别出os.system啊。
上论坛,要有帮助菜鸟的情怀,写一篇你对某些命名空间的使用心得也好啊。
性情乖张写啥我都不屑去看。
atlantis141 2011-04-01
  • 打赏
  • 举报
回复
解决问题了 在有空格的路径左右加双引号就好了.
iambic 2011-04-01
  • 打赏
  • 举报
回复
使用了os.system的代码是浪费别人的时间。在你放弃os.system之前,我不会阅读你的代码。
atlantis141 2011-04-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 iambic 的回复:]
C语言用户吗?为什么这么多人喜欢用os.system。看文档最后一段。

Python code
os.system(command)
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same……
[/Quote]

你能帮我看看代码有什么问题吗?是不是空格转义的问题,但是我不知道怎么改.用os.system()
iambic 2011-04-01
  • 打赏
  • 举报
回复
C语言用户吗?为什么这么多人喜欢用os.system。看文档最后一段。
os.system(command) 
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command.

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell after running command, given by the Windows environment variable COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always 0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status of the command run; on systems using a non-native shell, consult your shell documentation.

Availability: Unix, Windows.

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. Use the subprocess module. Check especially the Replacing Older Functions with the subprocess Module section.

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧