Hello all,
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile" )
data = file.read()
print "bytes read ", len(data)
---
But the bytes read is not being printed correctly, I think bytes are
being counted only till the first occurance of '\0' is encountered.
Even though the file is of a very large size, the bytes till the first
'\0' are counted.
Can someone pls advise me regarding this.
Thanks.
Best Regards,
Vineeth. 9 3848
vineeth wrote:
Hello all,
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile" )
data = file.read()
print "bytes read ", len(data)
---
But the bytes read is not being printed correctly, I think bytes are
being counted only till the first occurance of '\0' is encountered.
Even though the file is of a very large size, the bytes till the first
'\0' are counted.
I doubt that. Python doesn't interpret data when reading, and byte-strings
don't have a implicit 0-based length.
So I think you must be doing something different - clearly the above is not
actual code, but something made up for this post. Show us your actual code,
please.
diez
On Thu, 13 Dec 2007 04:04:59 -0800, vineeth wrote:
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile" )
data = file.read()
print "bytes read ", len(data)
---
But the bytes read is not being printed correctly, I think bytes are
being counted only till the first occurance of '\0' is encountered.
Even though the file is of a very large size, the bytes till the first
'\0' are counted.
If you want to deal with bytes better open the file in binary mode.
Windows alters line endings and stops at a specific byte (forgot the
value) otherwise.
Ciao,
Marc 'BlackJack' Rintsch
On Dec 13, 11:04 pm, vineeth <nvine...@gmail .comwrote:
Hello all,
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile" )
data = file.read()
print "bytes read ", len(data)
---
But the bytes read is not being printed correctly, I think bytes are
being counted only till the first occurance of '\0' is encountered.
Even though the file is of a very large size, the bytes till the first
'\0' are counted.
Can someone pls advise me regarding this.
Thanks.
Best Regards,
Vineeth.
On Dec 13, 5:13 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
vineeth wrote:
Hello all,
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile" )
data = file.read()
print "bytes read ", len(data)
---
But the bytes read is not being printed correctly, I think bytes are
being counted only till the first occurance of '\0' is encountered.
Even though the file is of a very large size, the bytes till the first
'\0' are counted.
I doubt that. Python doesn't interpret data when reading, and byte-strings
don't have a implicit 0-based length.
So I think you must be doing something different - clearly the above is not
actual code, but something made up for this post. Show us your actual code,
please.
diez
Hi,
The program tries to create a C Byte array of HEX data from a binary
input file (for ex : to embed a .png image with the application as an
array),
Here is the program :
"""
python script to create a bit stream of a input binary file.
Usage : bit_stream_crea tor.py -i input_file -b bytes_to_dump
"""
import sys
from binascii import hexlify
from optparse import OptionParser
if len(sys.argv) != 5:
print "incorrect args, usage : %s -i input_file -b bytes_to_dump" %
(sys.argv[0])
sys.exit(0)
parser = OptionParser()
parser.add_opti on("-i", "--input", dest="inputfile name")
parser.add_opti on("-b", "--bytes", dest="bytes")
(options, args) = parser.parse_ar gs()
print "-i",options.inpu tfilename
print "-b",options.byte s
# open input file
infile = open(options.in putfilename)
# create the member variable name.
mem_var_name = options.inputfi lename
mem_var_name = mem_var_name.re place(' ','_')
mem_var_name = mem_var_name.re place('.','_')
outfile_c = open(mem_var_na me + ".c","w")
outfile_h = open(mem_var_na me + ".h","w")
# read the data.
print " Reading %d bytes..... " % (int(options.by tes))
bytes_reqd = int(options.byt es)
data = infile.read(byt es_reqd)
print "Bytes Read ", len(data)
# convert to hex decimal representation
hex_data = hexlify(data)
i = 0
# Write the c file with the memory dump.
outfile_c.write ( "unsigned char %s[%d] = {\n" %
(mem_var_name,b ytes_reqd) )
while i < len(hex_data):
outfile_c.write ( "0x%c%c" % ( hex_data[i],hex_data[i+1] ) )
i += 2
if i != len(hex_data):
outfile_c.write (",")
if i % 32 == 0:
outfile_c.write ("\n")
outfile_c.write ( "\n};\n" )
# Write the .h file with forward declaration.
cpp_macro = "__"+mem_var_na me.upper()+"_H_ _"
outfile_h.write ("#ifndef "+cpp_macro + "\n")
outfile_h.write ("#define "+cpp_macro + "\n")
outfile_h.write ( "//%s, size %d \n" % (mem_var_name,l en(data)) )
outfile_h.write ( "extern unsigned char %s[%d];\n" %
(mem_var_name,b ytes_reqd) )
outfile_h.write ("#endif //"+cpp_macro + "\n")
#close the files.
outfile_c.close ()
outfile_h.close ()
infile.close()
________
But len(data) never proceeds beyond the NULL character.
Any help and tips is appreciated.
Thanks and Regards,
Vineeth.
On Dec 13, 11:04 pm, vineeth <nvine...@gmail .comwrote:
Hello all,
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile" )
data = file.read()
print "bytes read ", len(data)
---
But the bytes read is not being printed correctly, I think bytes are
being counted only till the first occurance of '\0' is encountered.
Even though the file is of a very large size, the bytes till the first
'\0' are counted.
Python will not stop on reading '\0'. On Windows in text mode (the
default), '\r\n' will be converted to '\n', and it will stop on
reading ^Z aka chr(26) aka '\x1A'. If you don't want that to happen,
use open('somefile' , 'rb') to get binary mode.
On Dec 13, 5:27 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
On Thu, 13 Dec 2007 04:04:59 -0800, vineeth wrote:
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile" )
data = file.read()
print "bytes read ", len(data)
---
But the bytes read is not being printed correctly, I think bytes are
being counted only till the first occurance of '\0' is encountered.
Even though the file is of a very large size, the bytes till the first
'\0' are counted.
If you want to deal with bytes better open the file in binary mode.
Windows alters line endings and stops at a specific byte (forgot the
value) otherwise.
Ciao,
Marc 'BlackJack' Rintsch
Thanks, opening the file in binary mode helped, thanks a lot.
vineeth wrote:
parser.add_opti on("-b", "--bytes", dest="bytes")
This is an aside, but if you pass 'type="int"' to add_option, optparse
will automatically convert it to an int, and (I think), give a more
useful error message on failure.
--
A couple potential optimizations:
>
# create the member variable name.
mem_var_name = options.inputfi lename
mem_var_name = mem_var_name.re place(' ','_')
mem_var_name = mem_var_name.re place('.','_')
mem_var_name = options.inputfi lename.replace( ' ','_').replace( '.','_')
No need to assign it 3 times.
while i < len(hex_data):
outfile_c.write ( "0x%c%c" % ( hex_data[i],hex_data[i+1] ) )
i += 2
if i != len(hex_data):
outfile_c.write (",")
if i % 32 == 0:
outfile_c.write ("\n")
This could be re-written as such:
for x in xrange(0, len(hex_data), 32):
output_c.write( '%s\n' % ','.join(['0x%s'%''.join(['%c'%a for a in
hex_data[x:x+32][i:i+2]]) for i in xrange(0, 32, 2)]
On Dec 13, 7:50 pm, Chris <cwi...@gmail.c omwrote:
A couple potential optimizations:
# create the member variable name.
mem_var_name = options.inputfi lename
mem_var_name = mem_var_name.re place(' ','_')
mem_var_name = mem_var_name.re place('.','_')
mem_var_name = options.inputfi lename.replace( ' ','_').replace( '.','_')
No need to assign it 3 times.
while i < len(hex_data):
outfile_c.write ( "0x%c%c" % ( hex_data[i],hex_data[i+1] ) )
i += 2
if i != len(hex_data):
outfile_c.write (",")
if i % 32 == 0:
outfile_c.write ("\n")
This could be re-written as such:
for x in xrange(0, len(hex_data), 32):
output_c.write( '%s\n' % ','.join(['0x%s'%''.join(['%c'%a for a in
hex_data[x:x+32][i:i+2]]) for i in xrange(0, 32, 2)]
Thanks everyone for all the suggestions, and the optimizations, I am
still a newbie and not aware of these ,
thanks! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kristian Nybo |
last post by:
Hi,
I'm writing a simple image file exporter as part of a school project. To
implement my image format of choice I need to work with big-endian
bytes, where 'byte' of course means '8 bits', not 'sizeof(char)'. It
seems that I could use bitset<8> to represent a byte in my code --- if
you have a better suggestion, I welcome it --- but that still leaves me
with the question of how to write those bitsets to an image file as
big-endian bytes...
|
by: Orion |
last post by:
Hey,
I was wondering if it was possible to determine if you hit 'EOF' using
fseek? I'm using fseek to traverse through the file from start to end
and capturing the data into a linked list structure. However, my loop
doesn't seem to work well - it totally fumbles out actually:
while ((a = fseek(fp,0,SEEK_CUR)) == 0){
// code here
}
|
by: googler |
last post by:
Is there any C library function that returns the size of a given file?
Otherwise, is there a way in which file size can be determined in a C
program? I need to get this for both Linux and Windows platforms, so a
generic solution is what I am looking for.
Thanks for your help.
|
by: Sirin Azazi |
last post by:
I opened an image file (bmp, jpeg etc) using FileOpenDialog and getting its
resolution, height and width. Now I want to get type of the file (not
looking at the extension, but which is kept inthe header pf the file). But i
couldn't find a method to directly get it by .NET command.
Do I have to use some APIs? or do you know a method to find out the file
type / format??
Thanks.
|
by: ramesh |
last post by:
can any one help me to solve this one we need to write a c program,
Consider a file of size 5000 bytes. Open the file and read to a buffer
of size buf;
We have to read 1000 bytes only from the file at a time and after
displaying the file we have to lseek 500 bytes back in the file
And read file from 500 to 1000. we have to read this type upto end of
| |
by: David |
last post by:
I have a VB.NET 1.1 app that uses FileStream to read files. It opens a file,
reads about 4K "header" data bytes from the beginning of it, then does a
Seek into the file (the distance of the Seek being determined from the
"header" data) , reads exactly 12,000 bytes and then closes the file (and
then it repeats the above several thousand times with other files)...
In order to speed-up the app, based on advice in this and other NG's, I...
|
by: tubby |
last post by:
Silly question, but here goes... what's a good way to determine when a
file is an Open Office document? I could look at the file extension, but
it seems there would be a better way. VI shows this info in the files:
mimetypeapplication/vnd.oasis.opendocument.textPK
mimetypeapplication/vnd.oasis.opendocument.presentationPK
etc.
Not really a Python specific question but, how do you guys do this sort
of thing? I've figured out how to...
|
by: =?Utf-8?B?VGhvbWFzWg==?= |
last post by:
Hi,
Is it possible to read a file in reverse and only get the last 100 bytes in
the file without reading the whole file from the begining? I have to get info
from files that are in the last 100 bytes of the file and some of these files
are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on the
largest files and the other files take "forever" to get the last 100 bytes.
This is the code I have currently that works with...
|
by: Freddy Coal |
last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all
the file in a String.
I make this function for make that:
'--------------------------
Public Shared Function Read_bin(ByVal ruta As String)
Dim cadena As String = ""
Dim dato As Array
If File.Exists(ruta) = True Then
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 we have to send another system
| |