Python,当同时读写文件时,文本会很奇怪
当我尝试用Python同时读写一个.txt文件时,结果非常奇怪Python,当同时读写文件时,文本会很奇怪,python,Python,当我尝试用Python同时读写一个.txt文件时,结果非常奇怪 from sys import argv script, filename = argv print """The contents of this .txt file will now be erased. if you don don't wish to do this. Hit CTRL+C. if you want to do this. Just click ENTER. """ raw_input("> ")
from sys import argv
script, filename = argv
print """The contents of this .txt file will now be erased.
if you don don't wish to do this. Hit CTRL+C.
if you want to do this. Just click ENTER.
"""
raw_input("> ")
target = open(filename, 'w+') #opens the file to read+write simultaneously
newline1 = raw_input("the first line: ") #three lines of strings to add to the .txt file
newline2 = raw_input("the second line: ")
newline3 = raw_input("the third line: ")
target.write(("%s\n%s\n%s") % (newline1, newline2, newline3)) #writes the lines to the file
print "this is what's currently written inside the .txt file:\n"
print target.read() #reads the contents of the file
target.close() #closes and saves the file
但是文件的内容只是在.txt文件中读取这个jibberish,当它在程序中读取时,而不是3行简单的代码:
s· d Z d d l Z d e j f d „ ƒ YZ d e j f d „ ƒ YZ d e j f d „ ƒ YZ d e e j f d
„ ƒ YZ d e e j f d „ ƒ YZ d
„ Z d Z e j e ƒ Z
d S( sv Python Character Mapping Codec cp1252 generated from 'MAPPINGS/VENDORS/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 charmap_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 ( t __name__t
__module__R 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\cp1252.pyR s ( R R
t FalseR ( ( ( 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 s ( R R
R R ( ( ( s# C:\Python27\lib\encodings\cp1252.pyR s t StreamWriterc B s e Z RS( ( R R
( ( ( s# C:\Python27\lib\encodings\cp1252.pyR s t StreamReaderc B s e Z RS( ( R R
( ( ( 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 d t ƒ S( Nt namet cp1252R R t incrementalencodert incrementaldecodert streamreadert streamwriter( R t CodecInfoR R 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
t
charmap_buildR ( ( ( s# C:\Python27\lib\encodings\cp1252.pyt <module> s ÿ ó:›› ™øÌ pò åP§ü a^v– Qý LýHVzàý0DýI.*Èý ýJ²ã¨@ÏüðÒüK6=XÙü0Ùü NÆüü ËüOžç5˜¯üp¯ü±v%P¿õýüQöy€Íüàü•ëw@MýMýSž@3 èü˜õü_å’Ï`FýøDý ßEïÐþüü¹ý¥t€ìüXþüXžf9`âü0Ùü Z2ðÀãüˆÛü O°s÷ÀýhØü]B] ìüþü^ÒÏZ€ýðý_NÅô Èü §ü GbP€åüÐÞüDÆí OüXþüc20F@HýxGýH¯«ÐPBý ¬üeú¿ýüðüüf¾Àëü(üüeõ5¸óüóü ¼å»úXDý0Dý k®à áüÔülš¦ âü0¤ü n"éÍ@!ý8Aý½-ò¡àý¸ý q.&yxLýPLý s¦v8(×ü0Ôü ¾]ª[àÝü¸Ýü;
RE`ý ¬ü |I\ ýÐ ýBÃœ”`ýÀý 0õ Èü°¦ü fPãÑü@Íü‚ª¥¨¾õpðüƒª¥à¼õ0¤üMÕ—pJýHJý…JÃ1 ÍüpÐü ‡ÊÑÈ@îüè ýˆd, Ïü`Ñü ‹úDâ ý€ýŒêGœ€ýÀ@ýjhü ×üÔü½L ÀHýhHý Æ Q`öü8öü\¤è½õ`Ñüõ\} ý°Aý n¸ì™Àý° ý ˜ª¥ð¾õÀúü™ª¥Ø¾õ¨ùüŽÞõ`ý¨Iý›¦[ ý€DýœÂ© Nü¸Ýü†T !ýÈBýžª¥À¾õñü V$¶àìüøþü¡„½@ïüðý £ŒMÀæüˆñü¤2/,ØühØüÆK¬§ ý€ý¦†? ýðý ¨Šrxý° ý©†L
`Ûü8Ûü «þa
编辑:在我添加target.seek0行之后,它就工作了。但是,如果知道是什么导致了jibberish的出现,那还是很有趣的 您可能希望在搜索之前尝试使用file.flush
否则,保留文件的两个句柄:
file = open(filename, "w")
file.write(stuff)
file.close()
file = open(filename, "r")
file.read()
我不认为写和转读文件是好的做法。 < P>我也有同样的问题。当我问我的老师时,他说发生这种情况是因为我在写文件后试图读取文件,并且由于写入文件,指针移到了文件的末尾,但根据我们的预期,我们希望从一开始就读取文件,因此我们需要使用file_object.seek0,它将指针设置到第0个位置,并且工作正常
f=open('new.txt','w+')
f.write("i am new to python")
f.seek(0)
data=f.read()
print (data)
f.close()
在读取之前尝试target.seek0?如果不倒带文件位置标头,则无法先写入,然后读取。使用file.seek执行此操作file.seek0返回到开始处。在print target.read之前添加target.seek0将获得预期的输出。即使没有seek调用,我仍然可以在文本文件中看到正确的输出,我不确定您的文件中怎么会有这么多乱七八糟的内容。我认为试图读取文件是乱七八糟的原因。我只是在python 2.7.6中没有得到任何输出。这是一个问题还是一个答案?我删除的一些材料让它看起来像一个问题,但剩下的材料看起来像是我的答案。