Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
just a couple questions....
#1
Regarding file input/output...
Small project I am working on will require me to take a bunch of xxxx files, strip out various infos and create small subheaders with pointers to the data section...I know the complete format I need for the subheaders as well as all the offsets I need. However depending on the group of files the number of subheaders will var (the number is stored is the root outputs header).. Anyway, is it possible with pyhton to lets say, prompt the user for number of files and then... ummm better to kinda show I guess..
Lets say # of files is 20
I want to create a new file with my header it will include a text line, then subheaders from each of the 20 files with offset pointers to the start of data for that subheader
example below
Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000 4D 55 4C 54 49 54 45 58 06 00 02 00 02 00 00 00 MULTITEX........
00000010 00 00 00 00 01 00 00 00 A4 00 00 00 18 00 00 00 ........¤.......
00000020 00 00 00 00 00 00 00 00 FF FF FF FF FF FF FF FF ........ÿÿÿÿÿÿÿÿ
00000030 FF FF FF FF 8A 79 47 3C 01 00 00 00 63 6F 72 64 ÿÿÿÿŠyG<....cord
00000040 5F 62 6F 64 79 00 00 00 00 00 00 00 00 00 00 00 _body...........
00000050 00 00 00 00 00 00 00 00 00 00 00 00 63 6F 72 64 ............cord
00000060 5F 62 6F 64 79 00 00 00 00 00 00 00 00 00 00 00 _body...........
00000070 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 ................
00000080 00 01 00 00 00 00 00 00 20 00 00 00 08 08 08 08 ........ .......
00000090 05 00 00 00 FF FF FF FF 01 00 00 00 A4 00 00 00 ....ÿÿÿÿ....¤...
000000A0 36 AC 00 00 6¬..

in the above 0x24-0xa3 is a sub header(0x80 byes) 9c-9f is the data offset (obviously one subheader 000a4 becomes the offset) a0-a4 is the data size 00-34 is he MAIN header...17-1a is the data start off set calculated by # of files multiplied by 0x80 Plus the main header size(0x24) (needed before hand to know where to place the first data section in the file...
so as I was asking, I need to create this, reading one file at a time, into ONE single output file, I can figure out pretty much most of it, but am unfamiliar with python and the commands dealing with input/output and appending data through those commands
doesn't have to be spelled out, just some general examples /help/suggestions...it's basically taking xxx number of files, changing their format and storing together in a different format
I know I am long winded and probably confusing...lol
Reply
#2
ok, figured most of my stuff out, however...
How the flip do you store a 00 into another file??... (chr(var)) reads it as null/none and crashes..I need it to be an integer cause it is part of a count that gets increased and appended onto subheaders

roughly I am:
num = 0
for stuff in otherstuff:
do my header stuff
w.seek (my store location)
w.write (chr(num))
num = num + 1
it crashes with
Error:
w.write(chr(number)) ValueError: chr() arg not in range(256)

(May-04-2018, 02:33 PM)medievil Wrote: ok, figured most of my stuff out, however...
How the flip do you store a 00 into another file??... (chr(var)) reads it as null/none and crashes..I need it to be an integer cause it is part of a count that gets increased and appended onto subheaders

roughly I am:
num = 0
for stuff in otherstuff:
do my header stuff
w.seek (my store location)
w.write (chr(num))
num = num + 1
it crashes with
Error:
w.write(chr(number)) ValueError: chr() arg not in range(256)

thanks! I went back to edit it when I realized but edit button had gone, didn't know there was a time limit
Reply
#3
in case anyone else ever needs that answer I found it...
 w.write(struct.pack("<i", number))
that will write the single value in Hex form into a binary file
Reply
#4
Thanks for sharing the solution, glad you got it sorted :)
Reply
#5
(May-04-2018, 05:35 PM)j.crater Wrote: Thanks for sharing the solution, glad you got it sorted :)

yea, now I ran into the opposite issue, reading a single byte as hex (or even decimal) so I can do some math on it then write it to a new location
Reply
#6
ok I am pretty much all set, just one final question.. is it possible to get the file list (so i can auto open files in a directory) from a folder the user inputs??

I tried this but it didn't work
str = ""
str = raw_input("Enter the path of your file: ")

files_in_dir = [ n for n in glob.glob('str *.mtx*') if isfile(join('str *.mtx*',n)) ]
total = len([ n for n in glob.glob('str *.mtx*') if isfile(join('str *.mtx*',n)) ])
I can use the direct path instead of str and it works, BUT I need this to be portable,Not everyone would have the same directory setup or even drive letters

well as usual figured it out AFTER I asked..lol
str = ""
str = raw_input("Enter the path of your file: ")

files_in_dir = [ n for n in glob.glob(str + "\*.mtx*") if isfile(join(str + "\*.mtx*",n)) ]
total = len([ n for n in glob.glob(str + "\*.mtx*") if isfile(join(str + "\*.mtx*",n)) ])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A couple of questions Led_Zeppelin 1 920 Sep-30-2022, 07:05 PM
Last Post: deanhystad
  More than a couple problems. MurphysLaw 7 3,246 Apr-12-2021, 09:11 PM
Last Post: virginiaturner
  Need help understanding a couple of functions (encrypt,decrypt, int_to_bytes) xoani 0 1,995 Jun-09-2019, 03:25 PM
Last Post: xoani
  Couple of questions about modules Winfried 6 3,891 Aug-24-2018, 08:38 PM
Last Post: Winfried
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 38,826 Nov-25-2017, 06:21 AM
Last Post: heiner55
  A couple of questions about pip install Athenaeum 3 3,578 Oct-13-2017, 08:18 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020