473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

truncating a file from the top down

rbt
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
Jul 18 '05 #1
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

Jul 18 '05 #2
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>

Jul 18 '05 #3
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>


Jul 18 '05 #4
rbt
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!
Jul 18 '05 #5

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

Similar topics

4
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...
5
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...
1
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...
11
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...
3
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,...
3
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...
1
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...
3
RedSon
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,...
1
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
0
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
0
BarryA
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...
1
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...
0
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...

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.