Read from /dev/zero

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 148
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster

Read from /dev/zero

 
0
  #1
Nov 12th, 2007
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 148
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster

Re: Read from /dev/zero

 
0
  #2
Nov 13th, 2007
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
  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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,054
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 297
woooee woooee is offline Offline
Veteran Poster

Re: Read from /dev/zero

 
0
  #3
Nov 13th, 2007
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".
  1. for each_char in charout:
  2. print ord(each_char),
  3. print
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 148
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster

Re: Read from /dev/zero

 
0
  #4
Nov 14th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,054
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 297
woooee woooee is offline Offline
Veteran Poster

Re: Read from /dev/zero

 
0
  #5
Nov 14th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC