473,543 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading and removing first x bytes of a file

Hello,

I have some Macbinary files on a PC. I want to recursively read these
files and remove the first 128 bytes of the files if they contain the
macbinary header info. I know how to read directories recursively, but
how would I read the first 128 bytes of each file in the path?

Thanks,
Bart

Jul 18 '05 #1
6 5011
bart_nessux wrote:
I have some Macbinary files on a PC. I want to recursively read these
files and remove the first 128 bytes of the files if they contain the
macbinary header info. I know how to read directories recursively, but
how would I read the first 128 bytes of each file in the path?


os.listdir() can return a list of the names in the path. You
can use os.path.isfile( ) to check that a given name doesn't
refer to a subdirectory.

Once you've opened one of the files for reading, just use .read(128)
to get a string containing the first 128 bytes from the file.

-Peter
Jul 18 '05 #2
bart_nessux <ba*********@ho tmail.com> writes:
I have some Macbinary files on a PC. I want to recursively read these
files and remove the first 128 bytes of the files if they contain the
macbinary header info. I know how to read directories recursively, but
how would I read the first 128 bytes of each file in the path?


You can use the file object .seek() and .read() methods to move around
in the file and read parts of it.

There is an example in the "Input and Output" chapter of the Python
Tutorial.

--
Brian Gough

Network Theory Ltd,
Publishing the Python Manuals --- http://www.network-theory.co.uk/
Jul 18 '05 #3
Peter Hansen wrote:
bart_nessux wrote:
I have some Macbinary files on a PC. I want to recursively read these
files and remove the first 128 bytes of the files if they contain the
macbinary header info. I know how to read directories recursively, but
how would I read the first 128 bytes of each file in the path?

os.listdir() can return a list of the names in the path. You
can use os.path.isfile( ) to check that a given name doesn't
refer to a subdirectory.

Once you've opened one of the files for reading, just use .read(128)
to get a string containing the first 128 bytes from the file.

-Peter


Thanks Peter, the below code recursively read the first 128B... am I
right in saying that? If so, now that I can read these bytes from all
..bin files in a directory, what would be the safest and fastest way of
removing them?

Bart

import os

def pc_macbinary_fi x(path):
for root, dirs, files in os.walk(path):
for fname in files:
bin = os.path.splitex t(fname)
if bin[1] == '.bin':
macbinary = file(os.path.jo in(root,fname), 'rb').read(128)
print "The file named:", fname, "contains: ", macbinary, "\n."

path = raw_input("Abso lute path to the directory that contains the bin
files: ")
pc_macbinary_fi x(path)

Jul 18 '05 #4
bart_nessux wrote:
Thanks Peter, the below code recursively read the first 128B... am I
right in saying that?
Well, your indentation is screwed up, for one thing, so I can't
guarantee the code does what you want. I'll leave actually testing
it up to you...
If so, now that I can read these bytes from all
.bin files in a directory, what would be the safest and fastest way of
removing them?


Define 'safe' and describe how fast you want it to run. <wink>

Anyway, you can't actually "remove" bytes from the files, so
what you really need to do is then read the *rest* of the bytes
(i.e. keep the file open after the first read, and do a .read()
of the rest of the data) and then write the shortened data to
a temporary file (module tempfile can be helpful here), then
once that's worked, use os.remove to remove the old file, and
os.rename to rename the temp file to the same name as the old
file.

-Peter
Jul 18 '05 #5

"Peter Hansen" <pe***@engcorp. com> wrote in message
news:UL******** ************@po wergate.ca...
Anyway, you can't actually "remove" bytes from the files, so
what you really need to do is then read the *rest* of the bytes
(i.e. keep the file open after the first read, and do a .read()
of the rest of the data) and then write the shortened data to
a temporary file (module tempfile can be helpful here), then
once that's worked, use os.remove to remove the old file, and
os.rename to rename the temp file to the same name as the old
file.


IF the remainder of the file fits in memory, then it can 'moved up' by
rewriting same file after seeking to beginning of file. Don't know how
safe on MAC OSes (ie, degree to which write is atomic). Of course, if data
are important, one should have at least a few minutes of battery backup.

TJR


Jul 18 '05 #6
Terry Reedy wrote:
IF the remainder of the file fits in memory, then it can 'moved up' by
rewriting same file after seeking to beginning of file. Don't know how
safe on MAC OSes (ie, degree to which write is atomic). Of course, if data
are important, one should have at least a few minutes of battery backup.


I think it's a fair assumption that when the OP asked for a "safe"
way, he meant at least safer than that. If power failed in the
middle of the update, or a system crash occurred, the file could
well be unrecoverable.. .

-Peter
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
4471
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one line at a time and print it out. How can I do this? I wrote the code below but it's not correct since the fscanf reads one word (terminating in...
3
9498
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I...
6
43990
by: Neil Patel | last post by:
I have a log file that puts the most recent record at the bottom of the file. Each line is delimited by a \r\n Does anyone know how to seek to the end of the file and start reading backwards?
9
3589
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so that its buffer is bigger than this. I did this expecting the data to be read in one pass.
3
4690
by: Marco Herrn | last post by:
Hi, I have a text file with some lines in it. Now I want to iterate over this file and exchange some lines with some others. I tried this approach: try: myfile= file('myfile', 'r+') while 1:
16
3725
by: Jm.GlezdeRueda | last post by:
Hi all, Im trying to read a 24bit bmp with fread, and i have some problems.. I want to read the whole structure in one time, but i dont know why, it only reads the first member well.. I have two questions.. 1- why if i change fread(bmp1, sizeof(bmp1), 1, fin); to fread(bmp1, sizeof(struct bmp), 1, fin); i have a Segment violation ??
9
1812
by: Bill Woessner | last post by:
Suppose I have a structure, foo, which is a POD. I would like to read and write it to disk as follows: std::ofstream outs; foo bar; outs.write(reinterpret_cast<char*>(&bar), sizeof(foo)); .... std::ifstream ins; foo bar; ins.read(reinterpret_cas<char*>(&bar), sizeof(foo));
2
1240
by: Gerry | last post by:
Python 2.5, Windows XP. I have a 48-line text file written by a Windows python script, I try to read it as follows: f = open ("depstats.txt", "r", 0) for index, line in enumerate(f):
6
4214
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread function. Here a few lines: char *sAnv; .... sprintf(file_a, "%s/anverso.jpg", strDir);
0
7412
marktang
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...
0
7355
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...
0
7594
Oralloy
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. ...
0
7748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5892
agi2029
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...
1
5285
isladogs
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...
0
4900
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1830
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
1
979
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.