![]() |
| just a couple questions.... - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: just a couple questions.... (/thread-9910.html) |
just a couple questions.... - medievil - May-03-2018 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 ok, figured most of my stuff out, however... - medievil - May-04-2018 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 + 1it crashes with (May-04-2018, 02:33 PM)medievil Wrote: ok, figured most of my stuff out, however... thanks! I went back to edit it when I realized but edit button had gone, didn't know there was a time limit RE: just a couple questions.... - medievil - May-04-2018 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 RE: just a couple questions.... - j.crater - May-04-2018 Thanks for sharing the solution, glad you got it sorted :) RE: just a couple questions.... - medievil - May-04-2018 (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 RE: just a couple questions.... - medievil - May-05-2018 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 letterswell 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)) ]) |