首页 > 解决方案 > 使用 python 3 写入功能时添加的额外字符

问题描述

我正在从 Learn Python3 The Hard Way 做一个练习。这是以下示例:

from sys import argv
from os.path import exists

script, from_file, to_file = argv

indata = open(from_file).read()

out_file = open(to_file, 'w')
out_file.write(indata)

out_file.close()

如果我使用这些参数运行脚本:

python ex17.py test.txt new_file.txt

然后我在使用cat查看文件后得到以下信息:

This is a test file.਍ഀ

最后这些角色是什么?打开文件是记事本有两个不同大小的框。这会算作换行符还是其他什么?如果我在 Python 3 中编写文件,如何防止这些添加的字符出现?

标签: python-3.xio

解决方案


推荐阅读