擅长:python、mysql、java
<p>正如Martijn Pieters评论的那样,您的文件不是<code>utf-8</code>编码的,我已经重写了您的代码,使其更符合pep-8样式指南,更具可读性:</p>
<pre><code>print("Enter first the source file and then destination file with respective format")
t = input() # Enter source file
t1 = input() # Enter destination file
print("Enter the keyword which has to be copied into another file")
find = input() # Enter the keyword which has to be copied
with open(t) as f, open(t1, "a+") as f1: # Open the source file and open the destination file in append and read mode
copy_lines = False
for line in f.readlines():
if find in line:
copy_lines = True
if copy_lines:
f1.write(line)
print("Task completed")
</code></pre>
<p>变量名使用小写字母,用下划线<code>_</code>分隔单词。你知道吗</p>
<p>在赋值之间留一个空格,比如<code>copy_lines = False</code>而不是<code>copy_lines=False</code>,<code>#</code>和注释之间留一个空格,比如<code># mycomment</code>而不是<code>#mycomment</code>。你知道吗</p>
<p>使用<code>with</code>打开文件,因为它会自动关闭文件。你知道吗</p>