问题描述:
看到一本好书《我们仨》,想把它分段放到 txt 内容里面,这样就可以让金山打字通,定义进去,每天打字看书。
直接上代码(本来是想用 XPath 来分段的,没这么去操作)
# -*- coding: UTF-8 -*-
import os
def my_begin(path):
for i in range(1, 10):
path_to_create = path + '\\《我们仨》' + str(i) + '.txt'
try:
if os.path.isfile(path_to_create):
print(path_to_create + " ,已经存在")
else:
file = open(path_to_create, 'w', encoding='utf-8')
end_word = ":q"
print("输入 :q 才结结束接收: ")
user_content = ''
for line in iter(input, end_word):
user_content += line + "\n"
# user_content = input("请输入 " + path_to_create + " 文件 写入的内容:") # 换行不接收
# file = open(path_to_create, 'w')
# file.write(user_content, encodings='gbk')
file.write(user_content)
file.close()
except Exception as e:
print(e)
if __name__ == "__main__":
from_path = r'D:\TypeGame\我们仨'
my_begin(from_path)
问题解决:
(如图)
把自己下载来的 txt 文件,分段写入到 txt 文件里面。