1

I am opening a file and if a particular condition is met, opening another file and transferring the records from the first file to the second, then deleting them from the first. The code below works, except for the line f.truncate(), which is causing the error in this post's title. Can anyone tell me what I need to amend to make this work?

f = open(filename,"r")
d = f.readlines()
f.seek(0)
for i in d:
    e = i.split(',')
    if len(e) > 28:
        with open(filename2, "a") as text_file:
            text_file.write(i.encode('cp1252'))
        text_file.close
f.truncate()        
f.close()

2 Answers 2

4

You have to open the first file as "r+" in order to read and write, like so:

f = open(filename,"r+")
for line in f:
    e = line.split(',')
    if len(e) > 28:
        with open(filename2, "a") as text_file:
            text_file.write(line.encode('cp1252'))
f.seek(0)
f.truncate()        
f.close()

I have also made the code more efficient as it should iterate over the lines instead of copying them to the side. An even more efficient approach would be this:

with open(filename,"r+") as file1, open(filename2, "a") as file2:
    for line in f:
        if line.count(',') > 27:
            file2.write(line.encode('cp1252'))
    file1.truncate(0)

That way you don't reopen the file every iteration, and since you're not using the split values, you can just count the commas (,) and compare to 27 instead of 28.

Removing only the copied lines

If you wish to remove only the copied lines and not empty the file, you have no way other than copying the entire file to the memory or using a tempfile.

That's the way to do it by copying to memory:

with open(filename,"r+") as file1, open(filename2, "a") as file2:
    file_lines = file1.readlines()
    file1.seek(0)
    file1.truncate()
    for line in file_lines:
        if line.count(',') > 27:
            file2.write(line.encode('cp1252'))
        else:
            file1.write(line)

Please make sure to specify an encoding if you're using Python 3.X.

Sign up to request clarification or add additional context in comments.

4 Comments

im trying to get it so that only the copied lines are deleted, but when i use the first code above, it empties the entire first text file. how do i amend so only the selected lines are deleted?
@gdogg371 Updated my answer accordingly.
Your updated code is wrong and will corrupt the contents of file1.
@alexis added file1.truncate() accordingly.
0

Think of it as reading file1, splitting it in two, and writing the parts to file1 and file2. I wouldn't even bother with truncate() since opening a file for writing truncates it:

with open(filename1) as file1:
    data = file1.readlines()

lines1 = []
lines2 = []
for line in data:
    if len(line.split(",")) > 28:
        lines2.append(line)
    else:
        lines1.append(line)

with open(filename1, "w", encoding='cp1252') as outfile:   # Wipes out old contents
    outfile.writelines(lines1)

with open(filename2, "w", encoding='cp1252') as outfile: 
    outfile.writelines(lines2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.