The read and write python txt file open ()

Would try to study the document during the recording process will be executed during the execution file py into custom txt file can first try to open the non txt file, generally the following code:

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")

Error:

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'

So try to choose the same txt file operations:

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 and programs are stored on the desktop, but the problem can not be found, and I hope the problem can point big brother

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'
Released two original articles · won praise 0 · Views 53

Guess you like

Origin blog.csdn.net/qq_45960624/article/details/104562619