Tôi có một err "IOError: [Errno 0] Lỗi" với chương trình python này:Python tập tin hoạt động
from sys import argv
file = open("test.txt", "a+")
print file.tell() # not at the EOF place, why?
print file.read() # 1
file.write("Some stuff will be written to this file.") # 2
# there r some errs when both 1 & 2
print file.tell()
file.close()
những gì có vẻ là vấn đề? Những 2 trường hợp dưới đây là ok:
from sys import argv
file = open("test.txt", "a+")
print file.tell() # not at the EOF place, why?
# print file.read() # 1
file.write("Some stuff will be written to this file.") # 2
# there r some errs when both 1 & 2
print file.tell()
file.close()
và:
from sys import argv
file = open("test.txt", "a+")
print file.tell() # not at the EOF place, why?
print file.read() # 1
# file.write("Some stuff will be written to this file.") # 2
# there r some errs when both 1 & 2
print file.tell()
file.close()
vẫn còn, tại sao
print file.tell() # not at the EOF place, why?
không in kích thước của tập tin, là "a +" các append-mode ? sau đó con trỏ tập tin nên trỏ đến EOF?
Tôi đang sử dụng Windows 7 và Python 2.7.
Nơi nào bạn nhận được lỗi? Vấn đề có vẻ là bạn đang cố gắng đọc một tệp được mở trong chế độ chắp thêm – Dhara
Ngoài ra, bạn có chắc chắn rằng tệp text.txt tồn tại không? – Dhara
Mã của bạn hoạt động tốt cho tôi. 'tell' trả về' 0' ngay sau khi mở tập tin, tất nhiên, tại sao bạn lại mong đợi cái gì khác? –