Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 24th, 2008, 10:00 PM
eemqjunk eemqjunk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 2 eemqjunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 24 sec
Reputation Power: 0
Newbie: problems deleting lines from text file (large)

hello,

i'm a newbie programmer (and to python, double trouble :) and running into issues reading and writing files. i'm successfully running the code below to manipulate and remove the first few lines of a text file. The text file (file1.txt) is approx. 329kb but when i run the code it only writes a new 5kb file (file2.txt)... so the other 300 something kb below that is missing. how can i get the full file (to file2.txt) with only the first 4 lines removed???

so cofused... please help!! :)

lines = file('./simple-retrieval/file1.txt', 'r').readlines()
del lines[0:4]
file('./simpleunwrapped/file2.txt', 'w').writelines(lines)

Thanks in advance!!!

Reply With Quote
  #2  
Old April 25th, 2008, 04:13 AM
bvdet bvdet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 20 bvdet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 39 m 4 sec
Reputation Power: 0
Write a slice of variable lines to disk.
Python Code:
Original - Python Code
  1. lines[4:]
Example:
Python Code:
Original - Python Code
  1. >>> lines = [0,1,2,3,4,5,6,7,8,9,0]
  2. >>> lines = lines[4:]
  3. >>> lines
  4. [4, 5, 6, 7, 8, 9, 0]
  5. >>> 

Reply With Quote
  #3  
Old April 25th, 2008, 03:55 PM
eemqjunk eemqjunk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 2 eemqjunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 24 sec
Reputation Power: 0
thx for the reply...

i am able to delete the 4 lines.. its rewriting the complete file that i'm having issues with.

here's another thing i should throw in... if i comment out the "del lines" line the results are the same... the whole file is still not copying to the new location.

it looks like the whole file never really opens...

As far as opening the file... am i doing it correctly?? i had an impression that readlines() as opposed to readline() is suppose to read the whole file into memory... is this true?

Reply With Quote
  #4  
Old April 25th, 2008, 05:27 PM
c-2501 c-2501 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Location: Scotland
Posts: 28 c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 22 m 21 sec
Reputation Power: 0
I think you have to take another look at exactly the code you are using. I tried the following in the interactive shell (I should point out that the file is a lot smaller than the ones you mentioned though):

Python Code:
Original - Python Code
  1. f = open("somefile", 'r')
  2. lines = f.readlines()
  3. del lines[0:4]
  4. file("newfile.txt", "w").writelines(lines)


This seems to work, i.e it loads all the lines from the file into a list (your right that is what the readlines() function does, readline only reads in one line at a time), removes the first 4 lines (although I wouldent say this is the best way to do it), and then writes the lines to a new file.

The only thing I can think of is that the file is too large to be read in all at once, perhaps you should move to a solution using readline() and write the files one at a time to the new location, this will take more time, but I'm not sure what option you have.

Reply With Quote
  #5  
Old April 25th, 2008, 05:41 PM
c-2501 c-2501 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Location: Scotland
Posts: 28 c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 22 m 21 sec
Reputation Power: 0
Nope, just had a go at it with a much larger file than you are using (around 4Mb) and it works as expected. I don't know what else to suggest.

Reply With Quote
  #6  
Old April 25th, 2008, 05:46 PM
bvdet bvdet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 20 bvdet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 39 m 4 sec
Reputation Power: 0
When you write to a file, there needs to be some action that flushes the the output buffers. Create a file object, write to it, then close it.

Python Code:
Original - Python Code
  1. f = open(file_name, 'w')
  2. f.write(stuff)
  3. f.close()

Reply With Quote
  #7  
Old April 25th, 2008, 06:21 PM
c-2501 c-2501 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Location: Scotland
Posts: 28 c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 22 m 21 sec
Reputation Power: 0
Don't the buffers flush automatically when they become full? I also have a feeling (although I may be wrong), that writelines() flushes the buffer itself after each list item.

You should really call close() though, just make sure you don't do it before you need to read/write to the file in question.

Reply With Quote
  #8  
Old April 25th, 2008, 08:31 PM
bvdet bvdet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 20 bvdet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 39 m 4 sec
Reputation Power: 0
Yes, the buffers flush when full. I don't think any file method flushes the buffers automatically except for f.flush() and f.close(). You can control the buffering behavior of the file object with the optional bufsize parameter:
Python Code:
Original - Python Code
  1. f = open(file_name, mode, bufsize)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Newbie: problems deleting lines from text file (large)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway