オープン、読み取りと書き込みのpython txtファイル()

まず、一般的に非txtファイル、次のコードを開こうとすることができ、記録処理中の文書は、カスタムtxtファイルに実行ファイルPY中に実行されます勉強しようとするだろう。

path = "源码.py"                  #需要读取的路径
filename = "a.txt"               #读取后的数据存放点(默认此程序同目录)
with open(path,'r') as line_object:   #'r'只读模式,'a'附加模式
     lines = line_object.headline()
for content in lines:
     with open(filename,'a') as file_object: 
         files = file_object.write(content+"\n")

エラー:

Traceback (most recent call last):
  File "C:\Users\28654\Desktop\工算综合程序\kkk.py", line 6, in <module>
    lines = line_object.headline()
AttributeError: '_io.TextIOWrapper' object has no attribute 'headline'

だから、同じtxtファイル操作を選択してみてください:

pathfile = "a.txt"                 #需要读取的路径文件
filename = "b.txt"        #读取后的数据存放点(默认此程序同目录)
with open(pathfile,'r') as line_object:
    lines = line_object.headline()
for content in lines:
    with open(filename,'a') as file_object: 
        files = file_object.write(content+"\n")

A.TXT、B.TXTおよびプログラムはデスクトップ上に保存されているが、問題が見つからない、と私はこの問題は、兄を指すことを願って

Traceback (most recent call last):
  File "C:\Users\28654\Desktop\读取写入脚本.py", line 3, in <module>
    with open(pathfile,'r') as line_object:
FileNotFoundError: [Errno 2] No such file or directory: 'a.txt'
リリース元の2件の記事 ウォンの賞賛0 ビュー53

おすすめ

転載: blog.csdn.net/qq_45960624/article/details/104562619