请大仙们帮忙看看小弟写的读写文件的脚本
"""
读取并执行一个指定文件中的内容,
将执行的过程重新写入一个新的文件。
"""
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')