2012-06-24 27 views
13

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.

+0

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

+0

Ngoài ra, bạn có chắc chắn rằng tệp text.txt tồn tại không? – Dhara

+0

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? –

Trả lời

10

Python sử dụng chức năng fdio của fdio và chuyển chế độ làm đối số. Tôi giả sử bạn sử dụng các cửa sổ, vì @Lev nói rằng mã hoạt động tốt trên Linux.

Sau đây là từ các tài liệu fopen của cửa sổ, điều này có thể là một đầu mối để giải quyết vấn đề của bạn:

When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.

Vì vậy, giải pháp là để thêm file.seek() trước file.write() gọi. Để thêm vào cuối tệp, hãy sử dụng file.seek(0, 2).

Đối với các bạn tham khảo, file.seek hoạt động như sau:

To change the file object’s position, use f.seek(offset, from_what). The position is computed from adding offset to a reference point; the reference point is selected by the from_what argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current file position, and 2 uses the end of the file as the reference point. from_what can be omitted and defaults to 0, using the beginning of the file as the reference point.

[tham khảo: http://docs.python.org/tutorial/inputoutput.html]

Như đã đề cập bởi @lvc trong các ý kiến ​​và @Burkhan trong câu trả lời của mình, bạn có thể sử dụng phiên bản mới hơn chức năng mở từ io module. Tuy nhiên, tôi muốn chỉ ra rằng chức năng ghi không hoạt động giống hệt nhau trong trường hợp này - bạn cần cung cấp các chuỗi unicode như là đầu vào [Đơn giản chỉ cần tiền tố một u vào chuỗi trong trường hợp của bạn]:

from io import open 
fil = open('text.txt', 'a+') 
fil.write('abc') # This fails 
fil.write(u'abc') # This works 

Cuối cùng, vui lòng tránh sử dụng tên 'tệp' làm tên biến vì nó đề cập đến loại nội trang và sẽ được ghi âm thầm, dẫn đến một số lỗi khó phát hiện.

+0

'a +' cho phép đọc. –

+0

@LevLevitsky, tôi không thể chắc chắn về điều đó từ tài liệu, nhưng bạn nói đúng, mã hoạt động – Dhara

+0

Hơn nữa, nó cũng mở các tệp không tồn tại mà không có bất kỳ lỗi nào. –

6

Các giải pháp là sử dụng open từ io

D:\>python 
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on 
win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> f = open('file.txt','a+') 
>>> f.tell() 
0L 
>>> f.close() 
>>> from io import open 
>>> f = open('file.txt','a+') 
>>> f.tell() 
22L 
Các vấn đề liên quan