首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

请大仙们帮忙见见小弟写的读写文件的脚本

2013-07-01 
请大仙们帮忙看看小弟写的读写文件的脚本读取并执行一个指定文件中的内容,将执行的过程重新写入一个新

请大仙们帮忙看看小弟写的读写文件的脚本
"""
读取并执行一个指定文件中的内容,
将执行的过程重新写入一个新的文件。
"""
import os

file = open(r"C:\Learning Python\ping.txt","r",encoding="utf-8")
#new_file = open(r"C:\Learning Python\test.txt","w",encoding="utf-8")
for ping in file.readlines():
    new_file = open(r"C:\Learning Python\test.txt","a",encoding="utf-8")
    command = os.system("ping" + " " + ping)
    new_command = str(command,encoding='utf-8')
    new_file.writelines(command)


错误信息:

Traceback (most recent call last):
  File "C:/Learning Python/2121.py", line 18, in <module>
    new_command = str(command,encoding='utf-8')
TypeError: coercing to str: need bytes, bytearray or buffer-like object, int found


该如何修改


[解决办法]
os.system返回的是整数,所以要先把它变成str,再编码。


  new_command = str(command).encode('utf-8')

热点排行