Python : Delete certain lines from a file - python

I'm making a program which deletes certain lines from an existing file. It takes file1 as entry(f1), it looks for a certain pattern and if it finds it, it modifies the line (to make it compatible with the other file) and saves this modification in a variable 'mark'. It opens another file f2, and searches 'mark' in it. If it finds 'mark' in a certain line in f2, I have to delete that line and the three lines after. The thing is that when I run it, the program deletes everything from f2, so I get an empty file as a result.
new=''
pattern2 = '2:N:0:8'
i=0
f1=open('test_reverse.txt','r')
for line in f1:
if pattern2 in line:
mark=line.replace('2:N:0:8','1:N:0:8')
f2=open('test_OKforward2.txt','r')
lines=f2.readlines()
for i, line in enumerate(lines):
if mark in lines[i]:
e=lines[i]
e1=lines[i+1]
e2=lines[i+2]
e3=lines[i+3]
new=e+e1+e2+e3
f3=open('test_OKforward2.txt','w')
if line!=new:
f3.write(line)
I tried with the next() function as well, but I got the same result and a 'stop iteration' error.

The thing is that when I run it, the program deletes everything from f2, so I get an empty file as a result.
Whenever you open a file for writing, everything in it is lost. You have to re-write everything you wish to preserve in the files and exclude what you wanted to delete in the first place.
Notice these lines:
f2=open('test_OKforward2.txt','r')
# ...
f3=open('test_OKforward2.txt','w')
The problem is that f3 is opening the same file as f2 for writing for every loop on the lines of file f2.
Basically, after you add lines, you re-open the file for writing, eliminating what you had previously.
First: You should remove the f3=open from within the loop iterating on each line of f2 (i.e. do this at some other location outside this loop). This is the main issue.
Second: Use a temporary file for the process instead and, at the end, rename the temporary file to the one you had.
Third: You're not closing the files. Consider using context managers. Your code would look more like this:
with open('something.txt') as f2:
# do something with f2;
# f2 with be automatically closed when it exits the ctx manager
Fourth: Follow the PEP-8 style standards for your code. Everyone reading your code will thank you.
I got [...] a 'stop iteration' error.
This is normal; you said you were using the next() function. Iterators and next() raise StopIteration in order to signal that they cannot produce more elements from the collection being iterated and that this iteration process should stop.
Quoting the docs:
exception StopIteration
Raised by built-in function next() and an iterator‘s __next__() method to signal that there are no further items produced by the iterator.

Related

Precurse with open() or .write()?

Is there a way to precurse a write function in python (I'm working with fasta files but any write function that works with text files should work)?
The only way I could think is to read the whole file in as an array and count the number of lines I want to start at and just re-write that array, at that value, to a text file.
I was just thinking there might be a write an option or something somewhere.
I would add some code, but I'm writing it right now, and everyone on here seems to be pretty well versed, and probably know what I'm talking about. I'm an EE in the CS domain and just calling on the StackOverflow community to enlighten me.
From what I understand you want to truncate a file from the start - i.e remove the first n lines.
Then no - there is no way you can do without reading in the lines and ignoring the lines - this is what I would do :
import shutil
remove_to = 5 # Remove lines 0 to 5
try:
with open('precurse_me.txt') as inp, open('temp.txt') as out:
for index, line in enumerate(inp):
if index <= remove_to:
continue
out.write(line)
# If you don't want to replace the original file - delete this
shutil.move('temp.txt', 'precurse_me.txt')
except Exception as e:
raise e
Here I open a file for the output and then use shutil.move() to replace the input file only after the processing (the for loop) is complete. I do this so that I don't break the 'precurse_me.txt' file in case the processing fails. I wrap the whole thing in a try/except so that if anything fails it doesn't try to move the file by accident.
The key is the for loop - read the input file line by line; using the enumerate() function to count the lines as they come in.
Ignore those lines (by using continue) until the index says to not ignore the line - after that simply write each line to the out file.

how to copy python method text to text files

I want to extract each method of a class and write it to a text file. The method can be composed of any length of lines and the input file can contain any number of methods. I want to run a loop that will start copying lines to the output file when it hits the first def keyword and continue until the second def. Then it will continue until all methods have been copied into individual files.
Input:
class Myclass:
def one():
pass
def two(x, y):
return x+y
Output File 1:
def one():
pass
Output File 2:
def two(x, y):
return x+y
Code:
with open('myfile.py', 'r') as f1:
lines = f1.readlines()
for line in lines:
if line.startswith('def'): # or if 'def' in line
file = open('output.txt', 'w')
file.writelines(line)
else:
file.writelines(line)
It's unclear what exactly is your problem or what you have actively tried so far.
Looking at the code you supplied, there are somethings to point out:
Since you are iterating over lines obtained using readlines(), they already contain line breaks. Therefore when writing to file, you should use simply write() instead of writelines(), unless you want duplicated line breaks.
If the desired output is each function on a different file, you should create a new file each time you find an occurrence of "def". You could simply use a counter and increment it each time to create unique filenames.
Always make sure you are correctly dealing with files (opening and close or using with statement). In your code, it seems that file would no longer be opened in your else statement.
Another possible solution, based on Extract functions from python file and write them to other files (not sure if duplicate, as I am new here, but very similar question), would be:
Instead of reading each line of the file, you could read the entire file and then split it by "def" keyword.
Read the file to a String.
Split the string by "def" (make sure you don't end up without the "def" words) into a list.
Ignore the first element, since it will be everything before the first function definition, and iterate over the remaining ones.
Write each of those Strings (as they will be the function defs you want) into a new file (you could use a counter and increment it to produce a different name for each of the files).
Follow this steps and you should achieve your goal.
Let me know if you need extra clarification.

Reading files in Python with for loop

To read a file in Python, the file must be first opened, and then a read() function is needed. Why is that when we use a for loop to read lines of a file, no read() function is necessary?
filename = 'pi_digits.txt'
with open(filename,) as file_object:
for line in file_object:
print(line)
I'm used to the code below, showing the read requirement.
for line in file_object.read():
This is because the file_object class has an "iter" method built in that states how the file will interact with an iterative statement, like a for loop.
In other words, when you say for line in file_object the file object is referencing its __iter__ method, and returning a list where each index contains a line of the file.
Python file objects define special behavior when you iterate over them, in this case with the for loop. Every time you hit the top of the loop it implicitly calls readline(). That's all there is to it.
Note that the code you are "used to" will actually iterate character by character, not line by line! That's because you will be iterating over a string (the result of the read()), and when Python iterates over strings, it goes character by character.
The open command in your with statement handles the reading implicitly. It creates a generator that yields the file a record at a time (the read is hidden within the generator). For a text file, each line is one record.
Note that the read command in your second example reads the entire file into a string; this consumes more memory than the line-at-a-time example.

Remove lines in a text file after processing them in a loop

I have a simple program that processes some lines in a text file (adds some text to them). But then it saves them to another file. I would like to know if you can remove the line after the line is processed in the loop. Here is a example of how my program works:
datafile = open("data.txt", "a+")
donefile = open("done.txt", "a+")
for i in datafile:
#My program goes in here
donefile.write(processeddata)
#end of loop
datafile.close()
donefile.close()
As you can see, it just processes some lines from a file (separated by a newline). Is there a way to remove the line in the end of the loop so that when the program is closed it can continue where it left off?
Just so that I get the question right- you'd like to remove the line from datafile once you've processed and stored it in donefile ?
There is no need to do this and its also pretty risky to write to a file which is your source of read.
Instead , why not delete the donefile after you exit the loop? (i.e. after you close your files)
file iterator is a lazy iterator. So when you do for i in datafile it loads one line into memory at a time, so you are only working with that one line...so memory constraints shouldn't be of your concern
Lastly, to access files, please consider using with statement. It takes care of file handle exceptions and makes your program more robust

For loop in python: Code issue

I am somehow able to write the below code(taking help from various sources):
langs=['C','Java','Cobol','Python']
f1=open('a.txt','r')
f2=open('abc.txt','w')
for i in range(len(langs)):
for line in f1:
f2.write(line.replace('Frst languag','{}'.format(langs[i])))
f1.close()
f2.close()
Don't know why the the for loop is not running till the end. Because everytime i open the txt only 'C' gets stored in the txt. I want the script to run and at the end of the script's execution the txt should have the last value of the list (here python)
After the first pass of your inner for loop, f1 is pointing to the end of the file. So the subsequent passes don't do anything.
The easiest fix is to move f1=open('a.txt','r') to just before for line in f1:. Then the file will be re-read for each of your languages. (Alternatively, you might be able to restructure your logic so that you can handle all of the languages at the same time in one pass of the file.)

Categories

Resources