473,406 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How does f=open('mytext.txt', 'w+') work?

Rossum's tutorial on Python states:
"open() returns a file object, and is most commonly used with two
arguments: 'open(filename, mode)'
mode 'r+' opens the file for both reading and writing."

Here's a little session in Python's interactive window
f=open('mytext.txt','w+')
f.write('My name is Bob')
s=f.read()
s.len()
len(s) 4082f.close()


If I open the file mytext.txt in Notepad I see something that begins
with

"My name is Bob VwMÚ¸x¶ Ð"

and goes on for approximately 4082 characters.

What's happening??

Alex

Sep 18 '05 #1
6 5682
I have no idea what is happening, but to the subject line: I guess it's
a plain wrapper around fopen fron <stdio.h>
Sep 18 '05 #2
On Sun, 18 Sep 2005 09:11:51 -0700, Alex wrote:
Rossum's tutorial on Python states:
"open() returns a file object, and is most commonly used with two
arguments: 'open(filename, mode)'
mode 'r+' opens the file for both reading and writing."

Here's a little session in Python's interactive window
f=open('mytext.txt','w+')
f.write('My name is Bob')
s=f.read()
s.len()
len(s) 4082f.close()


If I open the file mytext.txt in Notepad I see something that begins
with

"My name is Bob VwMÚ¸x¶ Ð"

and goes on for approximately 4082 characters.

What's happening??


4082 is exactly 14 bytes less than four kilobytes. The string you wrote
to the file is... 14 bytes long.

Seems to me the file system allocated a 4K block to your file. You wrote
14 bytes to it, which advances the file pointer to byte 14, and then
read to the end of the file, which was filled with whatever random bytes
just happened to be on the disk in that place.

I don't get this behaviour under Linux, so I assume this is
Windows-specific. Under Linux, the new file is created with length 0, and
read() returns the empty string.

Any file has a "physical length" (how many blocks allocated on disk) and a
"logical length" (how many bytes are actually used). You should expect
that any time you create a new file, the initial contents could be
anything until you over-write it. This is not a problem when you create a
new file in ordinary write mode, because you can't read those existing
bytes, and when you close the file, that specifies the end-of-file.

You might find the truncate() method useful:

f=open('mytext.txt','w+')
f.write('My name is Bob')
f.truncate()
s = f.read()
# s should be the empty string -- untested because I'm not running Windows
f.seek(0)
s = f.read()
# s should be "My name is Bob"
f.close()

Hope this helps.
--
Steven.

Sep 18 '05 #3
"Alex" wrote:
If I open the file mytext.txt in Notepad I see something that begins
with

"My name is Bob VwMÚ¸x¶ Ð"

and goes on for approximately 4082 characters.

What's happening??


you're moving the file pointer around in a new file, and you're getting
junk (from the stdio file buffers, most likely) in the places where you
haven't written anything yourself.

</F>

Sep 18 '05 #4
18 Sep 2005 09:11:51 -0700, Alex <li*******@yahoo.se>:
Rossum's tutorial on Python states:


it's "Van Rossum's" :)
"van" in a part of the last name, you can't just cut it away in Dutch :)

--
Ksenia
Sep 18 '05 #5
Yes the problem seems to be exactly that. I'm moving around the file
pointer. This piece of code works. Observe the only thing I've added is
f.seek(0)
f=open('mytext.txt', 'w+')
f.write('My name is Bob')
f.seek(0)
s=f.read()
print s My name is Bob f.close()


I've found this piece of clue at
http://msdn.microsoft.com/library/de...c_._wfopen.asp
"However, when you switch between reading and writing, there must be an
intervening fflush, fsetpos, fseek, or rewind operation. The current
position can be specified for the fsetpos or fseek operation, if
desired."

Sep 18 '05 #6
Thanks Steven, very good explaination. f.seek(0) does the trick!
Alex

Sep 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peter | last post by:
L.S. I am developing a PHP-login script (on Lycos Tripod) that uses Session to pass on variables. Below is the entire (stripped) structure that I use. It opens a page where you can Set and Read...
3
by: cv | last post by:
Hello All, I have used MultipartRequest like the following to upload images. MultipartRequest multi = new MultipartRequest(request, "../webapps/coreprogram/dealerlogos", 1024 * 1024); It...
4
by: Field | last post by:
Hi, the following snippet shows once executed this output: 2 2 I'd have rather expected this output: 2 10
4
by: Das | last post by:
Hi, I have made an application in ASP.net with C#. The application works fine with localhost. I have uploaded the site. I'm using web user controls in the form. but some of the button do not work...
0
by: drumbum | last post by:
I need to know how to delet the resent work space and the open work space fromthe drop down menue
1
by: Daves | last post by:
a simple aspx page just needs to send a particular .txt file to stream so that it opens up in Notepad on client side. Using ContentType = "text/plain" it still opens in IE browser as a html file,...
1
by: Newbie in ChiTown | last post by:
Here's my code: I am using MS Access and I am trying to update a table (InvoiceDetails) with data input by the user on a form. However, it does not update nor does it give me an error message. ...
7
by: jacobevans | last post by:
i need some help opening a text file and having the contents in that file add to a list. for example a user types '/addlist C:/text.txt'. the program takes the c:/text.txt and adds the items to...
11
by: Jim | last post by:
Hi, I want to schedule a Python program that reads the command line for input. However, when adding an argument to the command line Python will not pick it up when using Windows scheduled...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.