Python 文件I/O有问题
我试着做一个简单的程序。它所要做的就是创建一个名为noah.txt的文件,并向其写入hello,然后将内容打印到其中。但是,当我运行它时,我的计算机开始发出嘟嘟声并输出胡说八道:Python 文件I/O有问题,python,file,Python,File,我试着做一个简单的程序。它所要做的就是创建一个名为noah.txt的文件,并向其写入hello,然后将内容打印到其中。但是,当我运行它时,我的计算机开始发出嘟嘟声并输出胡说八道: 0╥+☻ guru99.txt","w+") existing: f=open("guru99.txt", "a+") f = open("noah.txt","w+") f.write("hello") fl = f.readlines() for x in fl: print(x)
0╥+☻ guru99.txt","w+") existing: f=open("guru99.txt", "a+")
f = open("noah.txt","w+")
f.write("hello")
fl = f.readlines()
for x in fl:
print(x)
ä Z d♫ e☺ j â☺ Z YZ♠ d
d☻ S(☼ sv Python Character Mapping Codec cp1252 generated from 'MAPPINGS/V
ENDORS/MICSFT/WINDOWS/CP1252.TXT' with gencodec.py.
i Nt♣ Codecc ☻ B s e Z☺ d d☺ ä☺ Z☻ d d☻ ä☺ Z♥ RS(♥ t♠
strictc♥ ♥ ♦ C s‼ t j☺ |☺ |☻ t☻ â♥ S(☺ N(♥ t♠ codecst♫ charma
p_encodet♫ encoding_table(♥ t♦ selft♣ inputt♠ errors( ( s# C:\
Python27\lib\encodings\cp1252.pyt♠ encode♂ s☻ ☺c♥ ♥ ♦ C s‼ t j
☺ |☺ |☻ t☻ â♥ S(☺ N(♥ R☻ t♫ charmap_decodet♫ decoding_table(♥ R♣ R
♠ R ( ( s# C:\Python27\lib\encodings\cp1252.pyt♠ decode♫ s☻ ☺
(♦ __name__t
__module__ R♂ ( ( ( s# C:\Python27\lib\encodings\cp1252.pyR
s♦ ♠☻♀♥t↕ IncrementalEncoderc ☻ B s¶ e Z☺ e☻ d ä☺
Z♥ RS(☺ c♥ ♥ ♦ C s→ t j☺ |☺ | j☻ t♥ â♥ d☺ ↓S(☻ Ni (♦ R☻ R
♥ R R♦ (♥ R♣ R♠ t♣ final( ( s# C:\Python27\lib\encodings\c
t♣ False ( ( ( s# C:\Python27\lib\encodings\cp1252.pyR♫ ◄
s☻ ♠☺t↕ IncrementalDecoderc ☻ B s¶ e Z☺ e☻ d ä☺ Z♥ RS(☺ c♥
♥ ♦ C s→ t j☺ |☺ | j☻ t♥ â♥ d☺ ↓S(☻ Ni (♦ R☻ R R R
(♥ R♣ R♠ R☼ ( ( s# C:\Python27\lib\encodings\cp1252.pyR♂ ▬
R► R♂ ( ( ( s# C:\Python27\lib\encodings\cp1252.pyR◄ § s☻
( ( ( s# C:\Python27\lib\encodings\cp1252.pyR↕ ↓ s☻ ♠☺t♀ S
( ( ( s# C:\Python27\lib\encodings\cp1252.pyR‼ ∟ s☻ ♠☺c
☼ C s@ t j☺ d☺ d☻ d♥ t☻ â j♥ d♦ t☻ â j♦ d♣ t♣ d♠ t♠ d t â S(
Nt♦ namet♠ cp125 R♂ t↕ incrementalencodert↕ incrementaldecodert♀
streamreadert♀ streamwriter( R☻ t CodecInfoR R♂
R♫ R◄ R‼ R↕ ( ( ( s# C:\Python27\lib\encodings\cp1252.pyt♂
getregentry! s► ☺ ☺♠☺♀☺♀☺♠☺♠☺♠☺uû☺ ☺☻♥♦ ♠
♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]
^_`abcdefghijklmnopqrstuvwxyz{|}~⌂Γé¼∩┐╛ΓÇÜ╞ÆΓÇ₧ΓǪΓÇáΓÇí╦åΓÇ░┼áΓÇ╣┼Æ∩┐╛┼╜∩┐╛∩┐╛
‘’“”•–—˜™š›œ￾žŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³
´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛ
├£├¥├₧├ƒ├á├í├ó├ú├ñ├Ñ├ª├º├¿├⌐├¬├½├¼├¡├«├»├░├▒├▓├│├┤├╡├╢├╖├╕├╣├║├╗├╝├╜├╛├┐(♂ t
__doc__R☻ R R♫ R◄ R↕ R‼ R← R
charmap_buildR♦ ( ( ( s# C:\Python27\lib\encodings\cp1252.py <
module>♥ s¶ ♠☻♀↓♦↓♦∟♥∟♣ ☼♠ ♦ R◄ R↕ ( ( s∟ C:\Python27\lib\
functools.pyR‼ = s c☻ ☻ ☻ S s↨ | |☺ k♦ p§ | |☺ k☻ ♀S(☺ N(
(☻ R◄ R↕ ( ( s∟ C:\Python27\lib\functools.pyR‼ > s c☻ ☻
☻ S s▬ | |☺ k♦ p§ | |☺ k☻ S(☺ N( (☻ R◄ R↕ ( ( s∟ C
:\Python27\lib\functools.pyR‼ ? s c☻ ☻ ☻ S s♂ | |☺ k♦ ♀S(☺ N
( (☻ R◄ R↕ ( ( s∟ C:\Python27\lib\functools.pyR‼ @ s c☻
☻ ☻ S s↨ | |☺ k♣ ♀p▬ | |☺ k☻ S(☺ N( (☻ R◄ R↕ ( ( s∟
C:\Python27\lib\functools.pyR‼ A s c☻ ☻ ☻ S s↨ | |☺ k♣ o▬ |
|☺ k☻ ♀S(☺ N( (☻ R◄ R↕ ( ( s∟ C:\Python27\lib\functools.pyR
‼ B s c☻ ☻ ☻ S s♂ | |☺ k♣ ♀S(☺ N( (☻ R◄ R↕ ( (
s∟ C:\Python27\lib\functools.pyR‼ C s s6 must define at least one o
rdering operation: < > <= >=( t♥ sett♥ dirt
ValueErrort♥ maxR♥ R t♥ intR♦ R♠ (♠ t♥ clst convertt♣ roo
tst♦ roott♠ opnamet♠ opfunc( ( s∟ C:\Python27\lib\functools.pyt♫
total_ordering5 s. ☻♥☺♀☺♀☺‼☺♀☺♀☺‼☺♀☺♀☺‼☺♀☺♀☺▬☻∟☺♠☺☼☺♀☺↨☺♀☺ ☺§☺↨☺c☺
☻ ♦ ♥ s d☺ t f☺ ç f☺ d☻ å â Y}☺ |☺ S(♥ s, Convert a cmp= func
tion into a key= functiont☺ Kc ☻ ☻
Python:2.7.9默认值,2014年12月10日,12:28:03[MSC v.1500 64位AMD64]。Windows7 64位。为什么会发生这种情况?在我看来,这就像是打开一个文件,对其进行写入,然后尝试使用相同的文件句柄读取文件末尾以外的内容。嘟嘟声是终端/命令解释器被不可打印的字符击中,可能来自同一目录中的二进制*.pyc文件 Python可能根本不应该让您这样做,但我怀疑Windows上的底层IO库 这似乎是一个很好的答案: 你可能会这样做:
# new: f= open("guru99.txt","w+") existing: f=open("guru99.txt", "a+")
f = open("noah.txt","w+")
f.write("hello")
# Now reset back to the start of the file
f.seek(0)
fl = f.readlines()
for x in fl:
print(x)
f.close()
也可以阅读上下文管理器:此标记看起来非常可疑:P@Mitch修正:太好了!您可能想删除该问题。请在此处添加您的实际代码Noah,以及对正在发生的错误或问题的更好描述。请,始终记得在问题中逐字添加您的代码。感谢您认真对待这个问题,并且没有用一篇甚至不接近相同的帖子将其作为副本报告。添加seek使其工作正常!伟大的是的,链接的答案是不一样的,但它确实有很多有用的信息,使我怀疑你看到的行为。享受Python带来的乐趣!
# new: f= open("guru99.txt","w+") existing: f=open("guru99.txt", "a+")
f = open("noah.txt","w+")
f.write("hello")
# Now reset back to the start of the file
f.seek(0)
fl = f.readlines()
for x in fl:
print(x)
f.close()