473,509 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Troubles with file operations

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?
Expand|Select|Wrap|Line Numbers
  1. f = open('C:/users/username/Documents/python/workfiles/testfile', 'r+')
  2. f.write('teststring')
  3. s = f.read()
  4. print s
  5. f.close()
  6. f = open('C:/users/username/Documents/python/workfiles/testfile', 'w')
  7. testobject = ['string', 12, 12.3, ('tuple','scond')]
  8. pickle.dump(testobject, f)
  9. f.close()
  10. f = open('C:/users/username/Documents/python/workfiles/testfile', 'r')
  11. o = pickle.load(f)
  12. print o
  13. f.close()
Sep 28 '08 #1
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:
Expand|Select|Wrap|Line Numbers
  1. f = open('C:/test.txt', 'r')
  2. o = list(pickle.load(f))
  3. print o
  4. f.close()
  5.  
Hope this helps.
Sep 28 '08 #2
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?
Expand|Select|Wrap|Line Numbers
  1. f = open('C:/users/username/Documents/python/workfiles/testfile', 'r+')
  2. f.write('teststring')
  3. s = f.read()
  4. print s
  5. f.close()
  6. f = open('C:/users/username/Documents/python/workfiles/testfile', 'w')
  7. testobject = ['string', 12, 12.3, ('tuple','scond')]
  8. pickle.dump(testobject, f)
  9. f.close()
  10. f = open('C:/users/username/Documents/python/workfiles/testfile', 'r')
  11. o = pickle.load(f)
  12. print o
  13. 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:
Expand|Select|Wrap|Line Numbers
  1. f = open('test.txt', 'r+')
  2. print f.tell()
  3. f.seek(0, 2)
  4. print f.tell()
  5. f.write('teststring')
  6. print f.tell()
  7. f.flush()
  8. f.seek(0)
  9. print f.tell()
  10.  
  11. s = f.read()
  12. print s
  13. 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:
Expand|Select|Wrap|Line Numbers
  1. f = open('test.txt', 'r+')
  2. print f.tell()
  3. print f.tell()
  4. f.write('ZZZZZZZZZZ')
  5. print f.tell()
  6. f.flush()
  7. f.seek(0)
  8. print f.tell()
  9.  
  10. s = f.read()
  11. print s
  12. f.close()
The write occurs at the beginning of the file, and overwrites the characters at that position.
Sep 28 '08 #3
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.
Sep 29 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1947
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.
3
12368
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...
0
1350
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...
0
3910
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....
2
4499
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...
9
3445
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...
1
2792
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...
2
1452
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...
7
1612
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...
0
2158
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...
0
7342
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
7410
jinu1996
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...
1
7067
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
7505
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...
1
5060
isladogs
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...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
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 ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
440
bsmnconsultancy
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...

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.