Python 附加到在 class 中打开的文件
Python append to file opened inside a class
这是我的 python 代码。我正在尝试创建一个 class 文件
操纵。我使用了与此类似的结构 URL 但我无法附加到文件。
add_to_file.py------------
import os
import sys
class add_to_file(object):
def __init__(self, filename):
self.data_file = open(filename,'a')
def __enter__(self): # __enter__ and __exit__ are there to support
return self # `with self as blah` syntax
def __exit__(self, exc_type, exc_val, exc_tb):
self.data_file.close()
def __iter__(self):
return self
def __append__(s):
self.data_file.write(s)
__append__("PQR")
add_to_file("Junk")
结果--------------------
Traceback (most recent call last):
File "add_to_file.py", line 4, in <module>
class add_to_file(object):
File "add_to_file.py", line 15, in add_to_file
__append__("PQR")
File "add_to_file.py", line 14, in __append__
self.data_file.write(s)
NameError: global name 'self' is not defined
将def __append__(s):
更改为def __append__(self, s):
不清楚您到底想完成什么 — 它看起来有点像上下文管理器 class。我将 __append__()
重命名为 append()
因为以双下划线开头和结尾的方法只能由语言定义,我将你的 class 从 add_to_file
重命名为 AddToFile
按照PEP 8 - Style Guide for Python Code.
import os
import sys
class AddToFile(object):
def __init__(self, filename):
self.data_file = open(filename,'a')
def __enter__(self): # __enter__ and __exit__ are there to support
return self # `with self as blah` syntax
def __exit__(self, exc_type, exc_val, exc_tb):
self.data_file.close()
def __iter__(self):
return self
def append(self, s):
self.data_file.write(s)
with AddToFile("Junk") as atf:
atf.append("PQR")
with open("Junk") as file:
print(file.read()) # --> PQR
这是我的 python 代码。我正在尝试创建一个 class 文件 操纵。我使用了与此类似的结构 URL 但我无法附加到文件。
add_to_file.py------------
import os
import sys
class add_to_file(object):
def __init__(self, filename):
self.data_file = open(filename,'a')
def __enter__(self): # __enter__ and __exit__ are there to support
return self # `with self as blah` syntax
def __exit__(self, exc_type, exc_val, exc_tb):
self.data_file.close()
def __iter__(self):
return self
def __append__(s):
self.data_file.write(s)
__append__("PQR")
add_to_file("Junk")
结果--------------------
Traceback (most recent call last):
File "add_to_file.py", line 4, in <module>
class add_to_file(object):
File "add_to_file.py", line 15, in add_to_file
__append__("PQR")
File "add_to_file.py", line 14, in __append__
self.data_file.write(s)
NameError: global name 'self' is not defined
将def __append__(s):
更改为def __append__(self, s):
不清楚您到底想完成什么 — 它看起来有点像上下文管理器 class。我将 __append__()
重命名为 append()
因为以双下划线开头和结尾的方法只能由语言定义,我将你的 class 从 add_to_file
重命名为 AddToFile
按照PEP 8 - Style Guide for Python Code.
import os
import sys
class AddToFile(object):
def __init__(self, filename):
self.data_file = open(filename,'a')
def __enter__(self): # __enter__ and __exit__ are there to support
return self # `with self as blah` syntax
def __exit__(self, exc_type, exc_val, exc_tb):
self.data_file.close()
def __iter__(self):
return self
def append(self, s):
self.data_file.write(s)
with AddToFile("Junk") as atf:
atf.append("PQR")
with open("Junk") as file:
print(file.read()) # --> PQR