Results 1 to 7 of 7

Thread: Remove the end of a text file

  1. #1
    Join Date
    Jun 2009
    Posts
    1,205

    Remove the end of a text file

    I've a small problem in python:

    I try to delete all the lines of a file after the last occurrence of a string (line where the string is included). I'm looking for a long time the approach to take to realize that when the module that could help me.

    Is there anyone who can help me to remove/delete lines of a file?

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Remove the end of a text file

    Do this:
    1. create a new file,
    2. copy all the old in the new except the end
    3. delete the old file and rename the new file for it is the name of the former.

  3. #3
    Join Date
    Nov 2008
    Posts
    1,221

    Re: Remove the end of a text file

    Open your file, the line passes in itertools.takewhile writing the lines out into a new file, the last operation optional is to delete the original file and rename the new.

    Code:
    $ python
    Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51)  
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import itertools
    >>> with open('as.txt','w') as f:
    ...     f.writelines(itertools.takewhile(lambda line: not line.lower().startswith('b'), open('/usr/share/dict/words')))
    ...  
    >>> ^D
     $ wc -l /usr/share/dict/words
      234936 /usr/share/dict/words
     $ wc -l as.txt  
       17061 as.txt
    in as.txt I have words that start with "a".

  4. #4
    Join Date
    Jun 2009
    Posts
    1,205

    Re: Remove the end of a text file

    Problems deleting rows I already had to go through something like that, the problem I have is to detect the last occurrence of a string in the file.

    I am reading the doc "re" but it does not seem to have what I want.

  5. #5
    Join Date
    Nov 2008
    Posts
    1,221

    Re: Remove the end of a text file

    This occurrence happens in all lines until it can no longer, or it is more rough estimates, like it can happen anywhere?

    And the string to check, is what kind, what a head it has?

  6. #6
    Join Date
    Jun 2009
    Posts
    1,205

    Re: Remove the end of a text file

    It can happen anywhere, and it is not all lines. It looks like:

    - 15:53:15 (logmount) TIMESTAMP 10/1/2010

    As the string "TIMESTAMP" is the only specific to this line, I went to the last TIMESTAMP detect the file and delete this line, then all that follows.

  7. #7
    Join Date
    Nov 2008
    Posts
    1,221

    Re: Remove the end of a text file

    The line itself should not be included? If you do so
    Code:
        from itertools import dropwhile
        with open('file.name.out', 'w') as output:
           with open('file.name') as input:
               output.writelines(reversed(
                   dropwhile(lambda line: 'TIMESTAMP' not in line,
                             reversed(input))))
    then there are surely mistakes that are not tested at all, but it should be a thing of style.

Similar Threads

  1. How to remove text from adobe acrobat file?
    By Raffaele in forum Windows Software
    Replies: 5
    Last Post: 25-05-2012, 08:52 PM
  2. Replies: 5
    Last Post: 13-01-2012, 05:18 PM
  3. How to convert image text into a text file
    By Wadee in forum Windows Software
    Replies: 5
    Last Post: 02-12-2010, 06:33 PM
  4. How to Export file and folder names to text file in MAC?
    By Colten in forum Operating Systems
    Replies: 6
    Last Post: 31-05-2010, 09:39 AM
  5. Reading text string from text file (PHP)
    By Moderate in forum Software Development
    Replies: 3
    Last Post: 16-01-2009, 03:27 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,657,864.17091 seconds with 17 queries