遇到文件I / O问题

时间:2017-03-04 01:41:54

标签: 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)

 ä  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        ☻   ☻

我的代码:

# new: f= open("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)
f.close()

Python:2.7.9(默认,2014年12月10日,12:28:03)[MSC v.1500 64 bit(AMD64)]。 Windows 7 64位。为什么会这样?

1 个答案:

答案 0 :(得分:2)

这让我觉得你正在打开一个文件,写入文件,然后尝试使用相同的文件句柄读取文件末尾。 (蜂鸣声是你的终端/命令解释器被一个不可打印的字符命中,可能来自同一目录中的二进制* .pyc文件。)

Python可能根本不应该让你这样做,但我怀疑Windows上的底层IO库。

这看起来很合理:Mixing read() and write() on Python files in Windows

您可以执行以下操作:

# 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()

也可以阅读上下文管理器(“with”):https://en.wikibooks.org/wiki/Python_Programming/Context_Managers