YarrOfDoom 1,247
Recognized Expert Top Contributor
I've started out on python a couple of weeks ago, and today I came to "7. Input and Output" in the python docs tutorial. So I tried some of it myself, but it always ends with completely mashed up output and error messages and my computer beeping like hell. So what is wrong with this code to cause all that? - f = open('C:/users/username/Documents/python/workfiles/testfile', 'r+')
-
f.write('teststring')
-
s = f.read()
-
print s
-
f.close()
-
f = open('C:/users/username/Documents/python/workfiles/testfile', 'w')
-
testobject = ['string', 12, 12.3, ('tuple','scond')]
-
pickle.dump(testobject, f)
-
f.close()
-
f = open('C:/users/username/Documents/python/workfiles/testfile', 'r')
-
o = pickle.load(f)
-
print o
-
f.close()
3 1435 boxfish 469
Recognized Expert Contributor
Looks like pickle's load function returns some kind of special pickle object with a weird way of printing. Try casting it to a list: -
f = open('C:/test.txt', 'r')
-
o = list(pickle.load(f))
-
print o
-
f.close()
-
Hope this helps.
bvdet 2,851
Recognized Expert Moderator Specialist
I've started out on python a couple of weeks ago, and today I came to "7. Input and Output" in the python docs tutorial. So I tried some of it myself, but it always ends with completely mashed up output and error messages and my computer beeping like hell. So what is wrong with this code to cause all that? - f = open('C:/users/username/Documents/python/workfiles/testfile', 'r+')
-
f.write('teststring')
-
s = f.read()
-
print s
-
f.close()
-
f = open('C:/users/username/Documents/python/workfiles/testfile', 'w')
-
testobject = ['string', 12, 12.3, ('tuple','scond')]
-
pickle.dump(testobject, f)
-
f.close()
-
f = open('C:/users/username/Documents/python/workfiles/testfile', 'r')
-
o = pickle.load(f)
-
print o
-
f.close()
The problem is not with pickle. The problem is that strange things can happen when reading and writing to a file and you don't flush the output buffer after a write. Try this: - f = open('test.txt', 'r+')
-
print f.tell()
-
f.seek(0, 2)
-
print f.tell()
-
f.write('teststring')
-
print f.tell()
-
f.flush()
-
f.seek(0)
-
print f.tell()
-
-
s = f.read()
-
print s
-
f.close()
The write is appended to the file. Using f.seek(0,2) places the current position at the end of the file. Now try this: - f = open('test.txt', 'r+')
-
print f.tell()
-
print f.tell()
-
f.write('ZZZZZZZZZZ')
-
print f.tell()
-
f.flush()
-
f.seek(0)
-
print f.tell()
-
-
s = f.read()
-
print s
-
f.close()
The write occurs at the beginning of the file, and overwrites the characters at that position.
YarrOfDoom 1,247
Recognized Expert Top Contributor
Thanks, not using flush() was indeed the problem, nothing was mentioned about it in the tutorial, guess they didn't expect people to try everything they explained there in one script.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: lmckaha |
last post by:
Hi,
Mysql version: 3.23.49
Solaris version: 2.7
gcc compiler version: 2.95.2
Python : 2.2.2
I'm evaluating the C and C++ API to decide which one to bye but I have
many troubles.
|
by: Peter |
last post by:
Hello,
Two newbie questions:
1) I have a javascript file with a function in it. From this function
I want to access a variable in another javascript file -which is not
inside a function. I...
|
by: Stefan Slapeta |
last post by:
Hi all,
I've experienced some troubles with message tables and wanted to know if
anybody knows a solution for one of them:
- If I translate my .mc file into a Unicode .bin file, some of the...
|
by: Lokkju |
last post by:
I am pretty much lost here - I am trying to create a managed c++
wrapper for this dll, so that I can use it from c#/vb.net, however, it
does not conform to any standard style of coding I have seen....
|
by: DEN |
last post by:
Hello,
I try to connect to an oracle DB though oci8 :
print "start<br>";
$conn = oci_connect($user,$base_Pass,$base);
if (!$conn) {
$oerr = OCIError($conn);
echo "Fetch Code 1:".$oerr;
print...
| |
by: Julien Biezemans |
last post by:
Hi!
Here is the problem: I'd like to restrict local filesystem stream
operations to one directory just like a root jail.
fopen('/file.bin') would actually open /some/path/file.bin.
One goal...
|
by: Andrea Gavana |
last post by:
Hello NG,
I am using the latest Numpy release 1.0rc2 which includes F2PY. I
have switched to Python 2.5 so this is the only alternative I have
(IIUC). With Python 2.4, I was able to build a very...
|
by: Joe324 |
last post by:
Hello,
I'm writing a program that will (eventually) read in a list of names from a file and store them in a linked list. I have the basics of the program done and it compiles with no errors, but...
|
by: greg |
last post by:
Thomas Philips wrote:
Have a look in
/Library/Frameworks/Python.framework/Versions/2.5
You can't -- this feature only exists in the Search window,
which is a different kind of window...
|
by: tabassumpatil |
last post by:
Please send the c code for:
1.STACK OPERATIONS :
Transfer the names stored in stack s1 to stack s2 and print the contents of stack s2.
2.QUEUE OPERATIONS :
Write a program to implement...
|
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,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |