Hi guys,
I need to truncate a file from the top down. I imagine doing something
like this:
if os.stat says the file is too big:
read the file
trim = only keep the last 2008 bytes (This is where I get stuck)
write trim back out to the original file
Would someone demonstrate the *best* most efficient way of doing this?
Thanks,
rbt 4 3691
rbt wrote: if os.stat says the file is too big: read the file trim = only keep the last 2008 bytes (This is where I get stuck) write trim back out to the original file
Would someone demonstrate the *best* most efficient way of doing this?
if os.stat says the_file is too big:
fh = open(the_file, 'rb')
fh.seek(2008, 2)
data = fh.read()
fh.close()
assert len(data)==2008 # you may want some error processing here
fh = open(the_file, 'wb')
fh.write(data)
fh.close()
/m
Mike Rovner wrote: if os.stat says the_file is too big: fh = open(the_file, 'rb') fh.seek(2008, 2)
should be
fh.seek(-2008, 2)
right?
data = fh.read() fh.close() assert len(data)==2008 # you may want some error processing here fh = open(the_file, 'wb') fh.write(data) fh.close()
or
if os.path.getsize(the_file) > TOO_BIG:
fh = open(the_file, 'rb+')
fh.seek(-2008, 2)
data = fh.read()
fh.seek(0) # rewind
fh.write(data)
fh.truncate()
fh.close()
</F>
Right. Thanks for the correction.
Fredrik Lundh wrote: Mike Rovner wrote:
if os.stat says the_file is too big: fh = open(the_file, 'rb') fh.seek(2008, 2)
should be
fh.seek(-2008, 2)
right?
data = fh.read() fh.close() assert len(data)==2008 # you may want some error processing here fh = open(the_file, 'wb') fh.write(data) fh.close()
or
if os.path.getsize(the_file) > TOO_BIG: fh = open(the_file, 'rb+') fh.seek(-2008, 2) data = fh.read() fh.seek(0) # rewind fh.write(data) fh.truncate() fh.close()
</F>
Mike Rovner wrote: Right. Thanks for the correction.
Fredrik Lundh wrote:
Mike Rovner wrote:
if os.stat says the_file is too big: fh = open(the_file, 'rb') fh.seek(2008, 2) should be
fh.seek(-2008, 2)
right?
data = fh.read() fh.close() assert len(data)==2008 # you may want some error processing here fh = open(the_file, 'wb') fh.write(data) fh.close() or
if os.path.getsize(the_file) > TOO_BIG: fh = open(the_file, 'rb+') fh.seek(-2008, 2) data = fh.read() fh.seek(0) # rewind fh.write(data) fh.truncate() fh.close()
</F>
Thanks for the info guys! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: qazmlp |
last post by:
Can anybody comment(& suggest improvements) on the following
implementation that is for
truncating the character buffer contents to the desired length?
Truncating char array
void...
|
by: VISHNU VARDHAN REDDY UNDYALA |
last post by:
Hello,
Can someone over here help me in truncating a float variable.
I mean if PI=3.14159 ...How can I get to read the first two or first
three decimal values with out rounding them. Any...
|
by: Jitesh Sinha |
last post by:
Hi,
I am running Windows 2003/ IIS 6.0. I was stuck with rather a abnormal
behaviour of System.Web.mail class. It was truncating the message body
after 3,071 character. The code i was testing...
|
by: Jason Williard |
last post by:
Is there a built-in function that I can use to truncate variables longer
than 30 characters? Even better, is there a function that truncates and
then appends "(...)" to the end?
--
Thank...
|
by: rangermccoy |
last post by:
Hello there,
What are the best php/c libraries for handling media including images,
video, and music?
I would like to manipulate media dfiles, including watermarking,
thumbnailing,...
|
by: Sonnich |
last post by:
Hi all!
I have 2 arrays of parts and assemblies. I check the part array whether
there is an assembly part in there, if so, 1) it should be moved to
the assembly array 2) its parts should be...
|
by: sconnors |
last post by:
I am currently working on an application that runs on Windows XP SP2 with .NET 2.0, Linux 2.4.19-16mdkenterprise and Solaris 5.9. The language is C++.
When I compare the output numbers, Windows...
|
by: RedSon |
last post by:
Here is the problem:
I have a path to a file like \Windows\Start Menu\Accessories\Calculator.lnk and I am trying to truncate it down to just "Calculator".
_tcscpy(pItem->label,...
|
by: rando1000 |
last post by:
I have a text file with a particularly long line about 2/3 of the way down the file. Whenever I import this, access thinks the longest record is 158, but in fact is more like 200. How do I convince...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |