Python Forum

Full Version: problems with writting a text file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi. I am quite new to Python. I create a new file then write something into it. the code is as below. I wonder why there are some texts written in the file that I didn't write at all?

the code I wrote:

>>> new='C:\\test\\1.txt'
>>> f=open(new,'w')
>>> f.close()
>>> f=open(new,'a')
>>> for i in range(10):
	f.write("this is line %d\r\n" % (i+1))
>>> f.close()
>>> f=open(new,'r')
>>> print f.read()
this is line 1
this is line 2
this is line 3
this is line 4
this is line 5
this is line 6
this is line 7
this is line 8
this is line 9
this is line 10

>>> f.close()
>>> f=open(new,'a+')
>>> f.write('Hello World')
>>> f.read()
'rdabil in Iran.\x03\x06\x02\x0c\x1bt\x14\x00\x00\x00OnDemandOutputWindowc\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00B\x00\x00\x00sE\x00\x00\x00e\x00\x00Z\x01\x00i\x02\x00i\x01\x00d\x00\x00d\x01\x006d\x02\x006i\x01\x00d\x03\x00d\x01\x006d\x04\x006Z\x02\x00d\x05\x00\x84\x00\x00Z\x03\x00d\x06\x00\x84\x00\x00Z\x04\x00d\x07\x00\x84\x00\x00Z\x05\x00RS(\x08\x00\x00\x00t\x04\x00\x00\x00bluet\n\x00\x00\x00foregroundt\x06\x00\x00\x00stdouts\x07\x00\x00\x00#007700t\x06\x00\x00\x00stderrc\x02\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00C\x00\x00\x00s\x16\x00\x00\x00|\x01\x00|\x00\x00_\x00\x00d\x00\x00|\x00\x00_\x02\x00d\x00\x00S(\x01\x00\x00\x00N(\x03\x00\x00\x00R4\x00\x00\x00R*\x00\x00\x00t\x04\x00\x00\x00owin(\x02\x00\x00\x00R\x08\x00\x00\x00R4\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s2\x00\x00\x00C:\\Python27\\ArcGIS10.4\\lib\\idlelib\\OutputWindow.pyR\x04\x00\x00\x00\x85\x00\x00\x00s\x04\x00\x00\x00\x00\x01\t\x01c\x04\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00C\x00\x00\x00s0\x00\x00\x00|\x00\x00j\x00\x00s\x16\x00|\x00\x00j\x01\x00\x83\x00\x00\x01n\x00\x00|\x00\x00j\x00\x00j\x02\x00|\x01\x00|\x02\x00|\x03\x00\x83\x03\x00\x01d\x00\x00S(\x01\x00\x00\x00N(\x03\x00\x00\x00RP\x00\x00\x00t\x05\x00\x00\x00setupR\x1d\x00\x00\x00(\x04\x00\x00\x00R\x08\x00\x00\x00R\x1a\x00\x00\x00R\x1b\x00\x00\x00R\x1c\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s2\x00\x00\x00C:\\Python27\\ArcGIS10.4\\lib\\idlelib\\OutputWindow.pyR\x1d\x00\x00\x00\x89\x00\x00\x00s\x06\x00\x00\x00\x00\x01\t\x01\r\x01c\x01\x00\x00\x00\x05\x00\x00\x00\x04\x00\x00\x00C\x00\x00\x00sx\x00\x00\x00t\x00\x00|\x00\x00j\x01\x00\x83\x01\x00\x04|\x00\x00_\x02\x00}\x01\x00|\x01\x00j\x03\x00}\x02\x00x6\x00|\x00\x00j\x04\x00j\x05\x00\x83\x00\x00D]%\x00\\\x02\x00}\x03\x00}\x04\x00|\x04\x00r/\x00|\x02\x00j\x06\x00|\x03\x00|\x04\x00\x8d\x01\x00\x01q/\x00q/\x00W|\x02\x00j\x07\x00d\x01\x00\x83\x01\x00\x01|\x00\x00j\x02\x00j\x08\x00|\x00\x00_\x08\x00d\x00\x00S(\x02\x00\x00\x00Nt\x03\x00\x00\x00sel(\t\x00\x00\x00R\x03\x00\x00\x00R4\x00\x00\x00RP\x00\x00\x00R\x05\x00\x00\x00t\x07\x00\x00\x00tagdefst\x05\x00\x00\x00itemst\r\x00\x00\x00tag_configuret\t\x00\x00\x00tag_raiseR\x1d\x00\x00\x00(\x05\x00\x00\x00R\x08\x00\x00\x00RP\x00\x00\x00R\x05\x00\x00\x00t\x03\x00\x00\x00tagt\x03\x00\x00\x00cnf(\x00\x00\x00\x00(\x00\x00\x00\x00s2\x00\x00\x00C:\\Python27\\ArcGIS10.4\\lib\\idlelib\\OutputWindow.pyRQ\x00\x00\x00\x8e\x00\x00\x00s\x0e\x00\x00\x00\x00\x01\x16\x01\t\x01\x1c\x01\x06\x01\x17\x01\r\x01(\x06\x00\x00\x00RG\x00\x00\x00RH\x00\x00\x00RS\x00\x00\x00R\x04\x00\x00\x00R\x1d\x00\x00\x00RQ\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s2\x00\x00\x00C:\\Python27\\ArcGIS10.4\\lib\\idlelib\\OutputWindow.pyRK\x00\x00\x00}\x00\x00\x00s\x0c\x00\x00\x00\x06\x02\x03\x02\x0e\x01\x11\x03\t\x04\t\x05(\x00\x00\x00\x00(\t\x00\x00\x00t\x07\x00\x00\x00Tkintert\x14\x00\x00\x00idlelib.EditorWindowR\x01\x00\x00\x00R-\x00\x00\x00R2\x00\x00\x00t\x07\x00\x00\x00idlelibR\x02\x00\x00\x00R\x03\x00\x00\x00RK\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s2\x00\x00\x00C:\\Python27\\ArcGIS10.4\\lib\\idlelib\\OutputWindow.pyt\x08\x00\x00\x00<module>\x01\x00\x00\x00s\x0c\x00\x00\x00\n\x01\x10\x01\x0c\x01\x0c\x01\x10\x02\x16vow.pyt\t\x00\x00\x00maybesave\x1c\x00\x00\x00s\x06\x00\x00\x00\x00\x02\x0c\x01\x04\x02t\x06\x00\x00\x00insertc\x04\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00C\x00\x00\x00ss\x00\x00\x00t\x00\x00|\x01\x00t\x01\x00\x83\x02\x00r<\x00y\x16\x00t\x02\x00|\x01\x00t\x03\x00j\x04\x00\x83\x02\x00}\x01\x00Wq<\x00\x04t\x05\x00k\n\x00r8\x00\x01\x01\x01q<\x00Xn\x00\x00|\x00\x00j\x06\x00j\x07\x00|\x03\x00|\x01\x00|\x02\x00\x83\x03\x00\x01|\x00\x00j\x06\x00j\x08\x00|\x03\x00\x83\x01\x00\x01|\x00\x00j\x06\x00j\t\x00\x83\x00\x00\x01d\x00\x00S(\x01\x00\x00\x00N(\n\x00\x00\x00t\n\x00\x00\x00isinstancet\x03\x00\x00\x00strt\x07\x00\x00\x00unicodeR\x02\x00\x00\x00t\x08\x00\x00\x00encodingt\x0c\x00\x00\x00UnicodeErrorR\x05\x00\x00\x00R\x12\x00\x00\x00t\x03\x00\x00\x00seet\x06\x00\x00\x00update(\x04\x00\x00\x00R\x08\x00\x00\x00t\x01\x00\x00\x00st\x04\x00\x00\x00tagst\x04\x00\x00\x00mark(\x00\x00\x00\x00(\x00\x00\x00\x00s2\x00\x00\x00C:\\Python27\\ArcGIS10.4\\lib\\idlelib\\OutputWindow.pyt\x05\x00\x00\x00write%\x00\x00\x00s\x10\x00\x00\x00\x00\x03\x0f\x01\x03\x01\x16\x01\r\x02\x07\x01\x16\x01\x10\x01c\x02\x00\x00\x00\x03\x00\x00\x00\x03\x00\x00\x00C\x00\x00\x00s"\x00\x00\x00x\x1b\x00|\x01\x00D]\x13\x00}\x02\x00|\x00\x00j\x00\x00|\x02\x00\x83\x01\x00\x01q\x07\x00Wd\x00\x00S(\x01\x00\x00\x00N(\x01\x00\x00\x00R\x1d\x00\x00\x00(\x03\x00\x00\x00R\x08\x00\x00\x00t\x05\x00\x00\x00linest\x04\x00\x00\x00line(\x00\x00\x00\x00(\x00\x00\x00\x00s2\x00\x00\x00C:\\Python27\\ArcGIS10.4\\lib\\idlelib\\OutputWindow.pyt\n\x00\x00\x00writelines2\x00\x00\x00s\x04\x00\x00\x00\x00\x01\r\x01c\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00C\x00\x00\x00s\x04\x00\x00\x00d\x00\x00S(\x01\x00\x00\x00N(\x00\x00\x00\x00(\x01\x00\x00\x00R\x08\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s2\x00\x00\x00C:\\Python27\\ArcGIS10.4\\lib\\idlelib\\OutputWindow.pyt\x05\x00\x00\x00flush6\x00\x00\x00s\x02\x00\x00\x00\x00\x01t\x03\x00\x00\x00Cuts\x07\x00\x00\x00<<cut>>t\x0f\x00\x00\x00rmenu_check_cutt\x04\x00\x00\x00Copys\x08\x00\x00\x00<<copy>>t\x10\x00\x00\x00rmenu_check_copyt\x05\x00\x00\x00Pastes\t\x00\x00\x00<<paste>>t\x11\x00\x00\x00rmenu_check_pastes\x0f\x00\x00\x00Go to file/lines\x12\x00\x00\x00<<goto-file-line>>s\x1a\x00\x00\x00file "([^"]*)", line (\\d+)s\x11\x00\x00\x00([^\\s]+)\\((\\d+)\\)s\x15\x00\x00\x00^(\\s*\\S.*?):\\s*(\\d+):s\x12\x00\x00\x00([^\\s]+):\\s*(\\d+):s\x15\x00\x00\x00^\\s*(\\S.*?):\\s*(\\d+):c\x02\x00\x00\x00\t\x00\x00\x00\x05\x00\x00\x00C\x00\x00\x00s\xf4\x00\x00\x00|\x00\x00j\x00\x00d\x00\x00k\x08\x00rQ\x00g\x00\x00}\x02\x00x-\x00|\x00\x00j\x02\x00D]"\x00}\x03\x00|\x02\x00j\x03\x00t\x04\x00j\x05\x00|\x03\x00t\x04\x00j\x06\x00\x83\x02\x00\x83\x01\x00\x01q\x1f\x00W|\x02\x00|\x00\x00_\x00\x00n\x00\x00|\x00\x00j\x07\x00j\x08\x00d\x01\x00d\x02\x00\x83\x02\x00}\x04\x00|\x00\x00j\t\x00|\x04\x00\x83\x01\x00}\x05\x00|\x05\x00s\xc5\x00|\x00\x00j\x07\x00j\x08\x00d\x03\x00d\x04\x00\x83\x02\x00}\x04\x00|\x00\x00j\t\x00|\x04\x00\x83\x01\x00}\x05\x00|\x05\x00s\xc5\x00t\n\x00j\x0b\x00d\x05\x00d\x06\x00d\x07\x00|\x00\x00j\x07\x00\x83\x02\x01\x01d\x00\x00Sn\x00\x00|\x05\x00\\\x02\x00}\x06\x00}\x07\x00|\x00\x00j\x0c\x00j\r\x00|\x06\x00\x83\x01\x00}\x08\x00|\x08\x00j\x0e\x00|\x07\x00\x83\x01\x00\x01d\x00\x00S(\x08\x00\x00\x00Ns\x10\x00\x00\x00insert linestarts\x0e\x00\x00\x00insert lineends\x17\x00\x00\x00insert -1line linestarts\x15\x00\x00\x00insert -1line lineends\x0f\x00\x00\x00No special linesT\x00\x00\x00The line you point at doesn\'t look like a valid file name followed by a line number.t\x06\x00\x00\x00master(\x0f\x00\x00\x00t\x0f\x00\x00\x00file_line_progst\x04\x00\x00\x00Nonet\x0e\x00\x00\x00file_line_patst\x06\x00\x00\x00appendt\x02\x00\x00\x00ret\x07\x00\x00\x00compilet\n\x00\x00\x00IGNORECASER\x05\x00\x00\x00t\x03\x00\x00\x00gett\x11\x00\x00\x00_file_line_helpert\x0c\x00\x00\x00tkMessageBoxt\t\x00\x00\x00showerrort\x05\x00\x00\x00flistt\x04\x00\x00\x00opent\x08\x00\x00\x00gotoline(\t\x00\x00\x00R\x08\x00\x00\x00t\x05\x00\x00\x00eventt\x01\x00\x00\x00lt\x03\x00\x00\x00patR\x1f\x00\x00\x00t\x06\x00\x00\x00resultR\n\x00\x00\x00t\x06\x00\x00\x00linenot\x04\x00\x00\x00edit(\x00\x00\x00\x00(\x00\x00\x00\x00s2\x00\x00\x00C:\\Python27\\ArcGIS10.4\\lib\\idlelib\\OutputWindow.pyR\x07\x00\x00\x00N\x00\x00\x00s(\x00\x00\x00\x00\x01\x0f\x01\x06\x01\x10\x01 \x01\x0c\x03\x15\x01\x0f\x01\x06\x03\x0c\x01\t\x01\x0f\x01\x06\x01\x06\x01\x03\x01\x06\x02\n\x01\x07\x01\x0c\x01\x12\x01c\x02\x00\x00\x00\x07\x00\x00\x00\x06\x00\x00\x00C\x00\x00\x00s\xaa\x00\x00\x00xz\x00|\x00\x00j\x00\x00D]k\x00}\x02\x00|\x02\x00j\x01\x00|\x01\x00\x83\x01\x00}\x03\x00|\x03\x00r\n\x00|\x03\x00j\x02\x00d\x01\x00d\x02\x00\x83\x02\x00\\\x02\x00}\x04\x00}\x05\x00y\x1e\x00t\x03\x00|\x04\x00d\x03\x00\x83\x02\x00}\x06\x00|\x06\x00j\x04\x00\x83\x00\x00\x01PWqu\x00\x04t\x05\x00k\n\x00rq\x00\x01\x01\x01q\n\x00qu\x00Xq\n\x00q\n\x00Wd\x00\x00Sy\x14\x00|\x04\x00t\x07\x00|\x05\x00\x83\x01\x00f\x02\x00SWn\x12\x00\x04t\x08\x00k\n\x00r\xa5\x00\x01\x01\x01d\x00\x00SXd\x00\x00S(\x04\x00\x00\x00Ni\x01\x00\x00\x00i\x02\x00\x00\x00t\x01\x00\x00\x00r(\t\x00\x00\x00R)\x00\x00\x00t\x06\x00\x00\x00searcht\x05\x00\x00\x00groupR5\x00\x00\x00t\x05\x00\x00\x00closet\x07\x00\x00\x00IOErrorR*\x00\x00\x00t\x03\x00\x00\x00intt\t\x00\x00\x00TypeError(\x07\x00\x00\x00R\x08\x00\x00\x00R\x1f\x00\x00\x00t\x04\x00\x00\x00progt\x05\x00\x00\x00matchR\n\x00\x00\x00R;\x00\x00\x00t\x01\x00\x00\x00f(\x00\x00\x00\x00(\x00\x00\x00\x00s2\x00\x00\x00C:\\Python27\\ArcGIS10.4\\lib\\idlelib\\OutputWindow.pyR1\x00\x00\x00i\x00\x00\x00s\x1e\x00\x00\x00\x00\x01\x10\x01\x0f\x01\x06\x01\x18\x01\x03\x01\x0f\x01\n\x01\x05\x01\r\x01\x0e\x02\x04\x01\x03\x01\x14\x01\r\x01(\x00\x00\x00\x00(\x03\x00\x00\x00s\x03\x00\x00\x00Cuts\x07\x00\x00\x00<<cut>>s\x0f\x00\x00\x00rmenu_check_cut(\x03\x00\x00\x00s\x04\x00\x00\x00Copys\x08\x00\x00\x00<<copy>>s\x10\x00\x00\x00rmenu_check_copy(\x03\x00\x00\x00s\x05\x00\x00\x00Pastes\t\x00\x00\x00<<paste>>s\x11\x00\x00\x00rmenu_check_pasteN(\x03\x00\x00\x00NNN(\x03\x00\x00\x00s\x0f\x00\x00\x00Go to file/lines\x12\x00\x00\x00<<goto-file-line>>N(\x10\x00\x00\x00t\x08\x00\x00\x00__name__t\n\x00\x00\x00__module__t\x07\x00\x00\x00__doc__R\x04\x00\x00\x00R\x0b\x00\x00\x00R\r\x00\x00\x00R\x11\x00\x00\x00R\x1d\x00\x00\x00R \x00\x00\x00R!\x00\x00\x00R*\x00\x00\x00t\x0b\x00\x00\x00rmenu_specsR+\x00\x00\x00R)\x00\x00\x00R\x07\x00\x00\x00R1\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s2\x00\x00\x00C:\\Python27\\ArcGIS10.4\\lib\\idlelib\\OutputWindow.pyR\x03\x00\x00\x00\x07\x00\x00\x00s*\x00\x00\x00\x06\x06\x06\x02\t\x06\t\x04\t\x03\t'
>>> f.close()
>>>
first of all, you would rarely find a case where you need to open and close a file multiple times.
Think of the file as a valise. what are the steps to pack one?
  • open the valise
  • Put shirt in valise
  • Put socks in valise
  • ...
  • close valise

Use same approach with a file:
  • open the file
  • write a record
  • write another record
  • ...
  • close the file

Please use BBcode tags: https://python-forum.io/misc.php?action=help&hid=25
So, you mean I get this problem since I several times opened and closed the file?
There's something speciously wrong there.
Not sure, but somewhere there is an obvious reason.

Here:
# open for appending at end of file
>>> f=open(new,'a+')
# Write at end of file
>>> f.write('Hello World')
# file pointer is now at end of file
>>> f.read()
you read beyond end of file
I would expect to see an end of file error, but there is none!
One thing I saw in testing is that append+ mode does not properly read the file because it's intended to write to and/or create the file. I tested it with a blank file and found that reading in append+ mode simply did not work. Reading in read mode was fine though. With my file though, it did not generate a long string of nonsense hex characters or mention a volcano in Iran. Maybe it's reading contiguous memory on the C: drive.
(Feb-23-2019, 11:01 PM)Larz60+ Wrote: [ -> ]I would expect to see an end of file error, but there is none!
file.read() never raises an exception, it simply returns an empty string if there's no data.
However, it shouldn't be reading that data either, whatever it might be.
This has been discussed with other admins and moderators, and we confirm that you have indeed found a bug.
The bug, however only exists in python 2.7 and was corrected in later versions. Python 2.7 will reach End of Life
this year, and the bug will probably not get fixed. So it's your option as whether to report it or not. Perhaps for legacy purposes it should be reported.

Should you choose to do so, you can file a bug report here: https://bugs.python.org/