Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Python Binary File Read and Parse

Shell Programming and Scripting

binaries, parse lines onto new file, python, solved

Thread Tools Search this Thread Display Modes
Old Unix and Linux 11-30-2012   -   Original Discussion by drd0spt
drd0spt drd0spt is offline
Registered User
Join Date: Nov 2012
Last Activity: 30 November 2012, 10:41 AM EST
Posts: 21
Thanks: 4
Thanked 0 Times in 0 Posts
Unix or Linux Question [SOLVED] Python Binary File Read and Parse

Hi to everyone Linux, i have a challenge right now in python that for now needs a bit of help in one part of the c0de.
The task is create a new file with the name of the file defined by the ASCII content between the 3 byte and the 16 byte that is parsed from the binary file, the file is over 20 Mb i need to create a file for each record ( each record contains the len of 820 ), so right now i know how to read it and do something with it, like :


with open("file", "rb") as f:
	byte = f.read()
	if byte > 820:
	    print "Reach the 1 record mark on the File you have defined "

Now the problem is , how do i get the name file that i want that is the string between the 2 byte and the 16 byte and how to save every 820 bytes to a new file like :

#attention noob code coming for quick understading i have a for loop for #example not for use i have to use it for the each 820 bytes new file
#with the read of the 820 bytes on the new file and save it and go for the
#next 820 bytes and so on until the end of the file.
for f.read(820) in file:
    if f.read() = 820:
       a = f.read()
       b = open("Iam_from_2_to_16_byte_string", w)

---------- Post updated 30-11-12 at 10:26 AM ---------- Previous update was 29-11-12 at 10:42 AM ----------

here is the solution :

n = 820
with open('/home/drdos/work/file.PDA', "rb") as f:
    while True:
        data = f.read(n)
        if not data:
        filename = str(data[16:32])
        x = open(filename, 'wb').write(data)

Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Read multiple files, parse data and append to a file empyrean Shell Programming and Scripting 8 09-27-2012 06:50 PM
Help to parse csv file with shell script Grhyll UNIX for Dummies Questions & Answers 4 01-19-2012 10:45 AM
Help w/ script to read file and parse log message shyork2001 Shell Programming and Scripting 2 04-06-2010 11:02 AM
Bash Script to read a file and parse each record 3vilwyatt Shell Programming and Scripting 8 02-25-2010 02:04 AM
How to read and parse the content of csv file containing # as delimeter into fields using Bash? barani75 Shell Programming and Scripting 4 02-22-2010 10:28 AM

All times are GMT -4. The time now is 04:44 AM.