"我的Python代码在write()函数中写入随机符号的原因 (Python 2.7)"

2024-06-16 10:48:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个Python代码:

f = open("database.txt", "r+")

f.write("Hello!")
print f.read()
f.close()

当我运行代码时,Idle会显示:-t

但是当我看到database.txt文件时,这是它的内部内容:

Hello!t   stdouts   #007700t   stderrc         C   s   | |  _  d  |  _ d  S(   N(   R4   R*   t   owin(   R   R4   (    (    s'   C:\Python27\lib\idlelib\OutputWindow.pyR   …   s      c         C   s0   |  j  s |  j ƒ  n  |  j  j | | | ƒ d  S(   N(   RP   t   setupR   (   R   R   R   R   (    (    s'   C:\Python27\lib\idlelib\OutputWindow.pyR   ‰   s        
c         C   sx   t  |  j ƒ |  _ } | j } x6 |  j j ƒ  D]% \ } } | r/ | j | |  q/ q/ W| j d ƒ |  j j |  _ d  S(   Nt   sel(    R   R4   RP   R   t   tagdefst   itemst
   tag_configuret      tag_raiseR   (   R   RP   R   t   tagt   cnf(    (    s'   C:\Python27\lib\idlelib\OutputWindow.pyRQ   Ž   s     
(   RG   RH   RS   R   R   RQ   (    (    (    s'   C:\Python27\lib\idlelib\OutputWindow.pyRK   }   s           (    (     t   Tkintert   idlelib.EditorWindowR   R-   R2   t   idlelibR   R   RK   (    (    (    s'   C:\Python27\lib\idlelib\OutputWindow.pyt   <module>   s   
vWindow.pyt   short_title   s    c         C   s   |  j  ƒ  r d Sd Sd  S(   Nt   yest   no(   t    get_saved(   R   (    (    s'   C:\Python27\lib\idlelib\OutputWindow.pyt    maybesave   s    t   insertc         C   ss   t  | t ƒ r< y t | t j ƒ } Wq< t k
 r8 q< Xn  |  j j | | | ƒ |  j j | ƒ |  j j  ƒ  d  S(   N(
   t
   isinstancet   strt   unicodeR   t   encodingt   UnicodeErrorR   R   t   seet   update(   R   t   st   tagst   mark(    (    s'   C:\Python27\lib\idlelib\OutputWindow.pyt   write%   s    
c         C   s"   x | D] } |  j  | ƒ q Wd  S(   N(   R   (   R   t   linest   line(    (    s'   C:\Python27\lib\idlelib\OutputWindow.pyt
   writelines2   s    
c         C   s   d  S(   N(    (   R   (    (    s'   C:\Python27\lib\idlelib\OutputWindow.pyt   flush6   s    t   Cuts   <<cut>>t   rmenu_check_cutt   Copys   <<copy>>t   rmenu_check_copyt   Pastes    <<paste>>t   rmenu_check_pastes   Go to file/lines   <<goto-file-line>>s   file "([^"]*)", line (\d+)s   ([^\s]+)\((\d+)\)s   ^(\s*\S.*?):\s*(\d+):s   ([^\s]+):\s*(\d+):s   ^\s*(\S.*?):\s*(\d+):c            C   sô   |  j  d  k rQ g  } x- |  j D]" } | j t j | t j ƒ ƒ q W| |  _  n  |  j j d d ƒ } |  j  | ƒ } | sÅ |  j j d d ƒ } |  j  | ƒ } | sÅ t
 j d d d |  j ƒd  Sn  | \ } } |  j j
 | ƒ } | j | ƒ d  S(   Ns   insert linestarts   insert lineends   insert -1line linestarts   insert -1line lineends   No special linesT   The line you point at doesn't look like a valid file name followed by a line number.t   parent(   t   file_line_progst   Nonet   file_line_patst   appendt   ret   compilet
   IGNORECASER   t   gett   _file_line_helpert   tkMessageBoxt     showerrort   flistt   opent   gotoline(     R   t   eventt   lt   patR   t   resultR
   t   linenot   edit(    (    s'   C:\Python27\lib\idlelib\OutputWindow.pyR   N   s(       
c         C   sª   xz |  j  D]k } | j | ƒ } | r
 | j d d ƒ \ } } y t | d ƒ } | j ƒ  PWqu t k
 rq q
 qu Xq
 q
 Wd  Sy | t | ƒ f SWn t k
 r¥ d  SXd  S(   Ni   i   t   r(       R)   t   searcht   groupR5   t   closet   IOErrorR*   t   intt      TypeError(   R   R   t   progt   matchR
   R;   t   f(    (    s'   C:\Python27\lib\idlelib\OutputWindow.pyR1   i   s    


(    (   R"   s   <<cut>>R#   (   R$   s   <<copy>>R%   (   R&   s     <<paste>>R'   N(   NNN(   s   Go to file/lines   <<goto-file-line>>N(   t   __name__t
   __module__t   __doc__R   R   R
   R   R   R    R!   R*   t   rmenu_specsR+   R)   R   R1   (    (    (    s'   C:\Python27\lib\idlelib\OutputWindow.pyR      s*                    
                t   OnDemandOutputWindowc           B   sE   e  Z i i d  d 6d 6i d d 6d 6Z d „  Z d „  Z d „  Z RS(   t   bluet
   fore

Tags: 代码libchecklinepytdatabaserpfile
3条回答

如果要写入文件,然后读取,则应:

以写入模式(w)打开。这也会将文件截断为零长度。你知道吗

f = open("database.txt", "w")
f.write("Hello!")
f.close

然后,以读取模式(r)打开它,将您定位在文件的开头:

f = open("database.txt", "r")
print f.read()
f.close()

使用“r+”会使您处于读/写模式,并且不会截断文件。所以,当你写信给它时,你的“你好!”字符串重写了文件的开头,其余部分保持不变。你知道吗

不过,最好使用with,如:

with open("database.txt", "w") as f:
    f.write("Hello!")

因此,您不必显式地关闭该文件,并且可以确保在发生错误时可以正确地关闭该文件。你知道吗

试着这样做:

with open("database.txt", "w") as f:
    f.write("Hello!")

with open("database.txt", "r") as f:
    for line in f:
        print line

您可以避免使用with statement手动关闭文件。 另外,一旦你写了文件,你就可以阅读它。 一般来说,最好将对文件的读写访问分开。除非你真的需要两者兼而有之。你知道吗

只有一个可能的原因(IMHO)-文件database.txt在你写信给它之前不是空的(尽管你在评论中声明了这一点)。你知道吗

因此,您的输出是O.K.,在开始处是字符串Hello!,其余的是文件的原始内容。你知道吗

所以你的f.read()从实际位置开始读取(用于读或写),它就在你写的东西之后,也就是在符号!之后。所以-在您的例子中-它将从t开始读取。你知道吗

在使用f.read()之前,您可以使用f.seek(position)更改实际位置。你知道吗

所以你的完整代码是:

f = open("database.txt", "r+")

    f.write("Hello!")
    f.seek(0)
    print f.read(6)                 # Note 6 as number of bytes to read
    f.close()

相关问题 更多 >