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 6 5021
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
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/
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)
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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 whitespace
or newline) at a time, instead of reading the whole line.
#include <stdio.h>
void...
|
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
have no idea of how big the byte array will need to be in the end (the
inflate and deflate methods...
|
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?
|
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.
|
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:
| |
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 ??
|
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));
|
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):
|
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);
|
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: 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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |