tags:

views:

558

answers:

6

The last line of my file is:

29-dez,40,

How can I modify that line so that it reads:

29-Dez,40,90,100,50

Note: I don't want to write a new line. I want to take the same line and put new values after 29-Dez,40,

I'm new at python. I'm having a lot of trouble manipulating files and for me every example I look at seems difficult.

A: 

Don't work with files directly, make a data structure that fits your needs in form of a class and make read from/write to file methods.

Toni Ruža
+4  A: 

Unless the file is huge, you'll probably find it easier to read the entire file into a data structure (which might just be a list of lines), and then modify the data structure in memory, and finally write it back to the file.

On the other hand maybe your file is really huge - multiple GBs at least. In which case: the last line is probably terminated with a new line character, if you seek to that position you can overwrite it with the new text at the end of the last line.

So perhaps:

f = open("foo.file", "wb")
f.seek(-len(os.linesep), os.SEEK_END) 
f.write("new text at end of last line" + os.linesep)
f.close()

(Modulo line endings on different platforms)

Douglas Leeder
A: 

good point... I know yet How to work with structures on python... but I will try to to that!!

for now I will try to follow your advice that is, re-writting the last line

Thank you guys. You are reaaly cool!!

UcanDoIt
This is not an answer. Please post this is a comment on a real answer or update your question with this. Feel free to delete this non-answer.
S.Lott
A: 

I have that error:

Traceback (most recent call last): File "poker.py", line 75, in ? InserSession() File "poker.py", line 29, in InserSession f.seek(-1,os.SEEK_END) # -2 on Windows? AttributeError: 'module' object has no attribute 'SEEK_END'

How can I fix that?

@Douglas Leeder How can I do a list that contain every lines:

maybe like:

for Line in file:

i=0

List[i]=Line

i++

this will be like a data structure of Lines?

UcanDoIt
Post it as another question.
J.F. Sebastian
+1  A: 

To expand on what Doug said, in order to read the file contents into a data structure you can use the readlines() method of the file object.

The below code sample reads the file into a list of "lines", edits the last line, then writes it back out to the file:

#!/usr/bin/python

MYFILE="file.txt"

# read the file into a list of lines
lines = open(MYFILE, 'r').readlines()

# now edit the last line of the list of lines
new_last_line = (lines[-1].rstrip() + ",90,100,50")
lines[-1] = new_last_line

# now write the modified list back out to the file
open(MYFILE, 'w').writelines(lines)

If the file is very large then this approach will not work well, because this reads all the file lines into memory each time and writes them back out to the file, which is very inefficient. For a small file however this will work fine.

Jay
A: 

I recently wrote a script to do something very similar to this. It would traverse a project, find all module dependencies and add any missing import statements. I won't clutter this post up with the entire script, but I'll show how I went about modifying my files.

import os
from mmap import mmap

def insert_import(filename, text):
    if len(text) < 1:
        return
    f = open(filename, 'r+')
    m = mmap(f.fileno(), os.path.getsize(filename))
    origSize = m.size()
    m.resize(origSize + len(text))
    pos = 0
    while True:
        l = m.readline()
        if l.startswith(('import', 'from')):
            continue
        else:
            pos = m.tell() - len(l)
            break
    m[pos+len(text):] = m[pos:origSize]
    m[pos:pos+len(text)] = text
    m.close()
    f.close()

Summary: This snippet takes a filename and a blob of text to insert. It finds the last import statement already present, and sticks the text in at that location.

The part I suggest paying most attention to is the use of mmap. It lets you work with files in the same manner you may work with a string. Very handy.