944,098 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2659
  • Python RSS
Nov 12th, 2007
0

Read from /dev/zero

Expand Post »
I'm curious why this bit of code doesn't work. When run it returns a blank line. Note: this is making use of a linux device file so it should not be expected to run properly on Windows.
python Syntax (Toggle Plain Text)
  1. #!/usr/bin/env python
  2. import os
  3.  
  4. infile = os.open("/dev/zero", os.O_RDONLY)
  5. charout = os.read(infile,1)
  6. os.close(infile)
  7. print charout

I'm pretty new to Python so I might be way off base. I appreciate any suggestions.
Last edited by mn_kthompson; Nov 12th, 2007 at 5:21 pm.
Similar Threads
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007
Nov 13th, 2007
0

Re: Read from /dev/zero

OK, I have been able to verify that something is being read from my device file, but nothing is printable. I have modified the code to read as follows
python Syntax (Toggle Plain Text)
  1. #!/usr/bin/env python
  2. import os
  3.  
  4. charout = "XXX"
  5. print len(charout)
  6. infile = os.open("/dev/zero", os.O_RDONLY)
  7. charout = os.read(infile,51)
  8. os.close(infile)
  9. print charout
  10. print len(charout)

And here is the output:
kevin@MSU1375020:~$ python read.py
3

51

So you can see that the charout file is getting 51 bytes of data, but it still wont print anything but a blank line. Does this give anyone any clues as to why I'm not getting a bunch of zeros on my screen?
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007
Nov 13th, 2007
0

Re: Read from /dev/zero

They are not printable characters (probably) and so who knows where they come from. To test this, try printing the decimal value of each character. It might just be a bunch of decimal zeroes (which is zero, the printable zero is decimal 48), or EOFs, or garbage created by some sort of "noise".
Python Syntax (Toggle Plain Text)
  1. for each_char in charout:
  2. print ord(each_char),
  3. print
Reputation Points: 741
Solved Threads: 693
Nearly a Posting Maven
woooee is online now Online
2,307 posts
since Dec 2006
Nov 14th, 2007
0

Re: Read from /dev/zero

Hey that worked for me, I got the zeros I was expecting. I guess when you read characters from a device file you might not be getting printable characters. I thought that python would have casted the characters into strings
Thanks!
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007
Nov 14th, 2007
0

Re: Read from /dev/zero

Zero evidently means that nothing was read and it will probably continue trying to read until you tell it to stop, which is why there are multiple zeros.
Reputation Points: 741
Solved Threads: 693
Nearly a Posting Maven
woooee is online now Online
2,307 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Verbal raw input?
Next Thread in Python Forum Timeline: Needing help with wxPython MediaCtrl





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC