How can I remove first line from fasta file? [duplicate] - python

This question already has answers here:
Read file from line 2 or skip header row
(8 answers)
Closed 8 years ago.
Structure of fasta file is like this:
>gi|568815364|ref|NT_077402.3| Homo sapiens chromosome 1 genomic scaffold, GRCh38 Primary Assembly HSCHR1_CTG1
TAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAAC
CCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCAACCCTAACCCTAACCCTAACCCTAACCCTAA
CCCTAACCCCTAACCCTAACCCTAACCCTAACCCTAACCTAACCCTAACCCTAACCCTAACCCTAACCCT
AACCCTAACCCTAACCCTAACCCCTAACCCTAACCCTAAACCCTAAACCCTAACCCTAACCCTAACCCTA
ACCCTAACCCCAACCCCAACCCCAACCCCAACCCCAACCCCAACCCTAACCCCTAACCCTAACCCTAACC
The first line is some information about the content of file and rest lines are strand of DNA, RNA or amino acid.
To do some work with this kind of file I need to remove first line of file. How can I do this using python?
I tried this code, but its not suitable:
My_string=open("SimpleFastaFile.fa", "r").read()
def line_remove(str):
if str.isalnum()==False:
str=str[1:]
line_remove(str)
line_remove(My_string)

you can use next to advanced pointer to nextline:
my_string = open("SimpleFsastaFile.fa", "r")
next(my_string) # advanced file pointer to next line
my_string.read()

If you need the whole file's content, why not read all lines at once and immediately slice away the first line?
with open('path','r') as f:
content = f.readlines()[1:]
output="".join(content)

Related

Remove current line from file [duplicate]

This question already has answers here:
Is it possible to modify lines in a file in-place?
(5 answers)
Closed 2 years ago.
Consider a file with the following lines:
remove
keep
remove
Is it possible to remove the current line while iterating the file lines?
for word in file:
if word != "keep":
remove_line_from_file
In the end the file should just the line with word keep.
I know I could create a file with the remaining words but I was hoping to keep the same file.
Python has a nice library named fileinput which allows you to modify files inplace. You can print what you want to keep back into the file:
with fileinput.input(filename, inplace=True) as lines:
for line in lines:
if line == 'keep':
print(line,)
No, but you can extract all the contents of the file beforehand, modify the text, and then rewrite them back into the file:
with open('file.txt','r') as f:
lines = f.readlines()
with open('file.txt','w') as f:
f.write(''.join([ln for ln in lines if 'keep' in ln])) # Writes all the lines back to file.txt that has the word `keep` in them

python append a new line to end of huge file after the last non-whitepsace lines [duplicate]

This question already has answers here:
Most efficient way to modify the last line of a large text file in Python
(2 answers)
Get last n lines of a file, similar to tail
(35 answers)
Closed 3 years ago.
I am currently appending like this:
with open(filename, "a") as fh:
fh.write("some line")
This assumes that the last line of the file is a new line and that it is empty
in the case where the file doesn't end with a new line, my code appends to the last line
i.e.
last line of textsome line
and not:
last line of text
some line
in the case where the file ends with many "empty" new lines, the line is added after them , which isn't good either.
i.e.
...
...
last line
\n
\n
\n
...
some line
I want my line to be added on a new line after the last meaningful existing line (line with text that is non-empty)
The text files are quite large, 10-20 gigs each , so I can't easily "read" and then "chomp".
I know that the files won't have more then 1-10 empty lines at the end (usually only a single empty line at the end hence the code usually works and rarely fails)
is there a variation on open (file, 'a') to seek to end of content of file on a new line?

How to read each line in a file backwards using Python [duplicate]

This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 3 years ago.
I'm trying to read a file (example below), line by line, backwards using Python.
abcd 23ad gh1 n d
gjds 23iu bsddfs ND31 NG
Note: I'm not trying to read the file from the end to the beginning, but I want to read each line starting from the end, i.e d for line 1, and NG for line 2.
I know that
with open (fileName) as f:
for line in f:
reads each line from left to right, I want to read it from right to left.
Try this:
with open(fileName, 'r') as f:
for line in f:
for item in line.split()[::-1]:
print(item)
If your file is not too big, you can read lines in reverse easily
with open(fileName) as f:
for line in reversed(f.readlines()):
# do something
Otherwise, I believe you'd have to use seed.

How to save line by line a file.txt in array or table? [duplicate]

This question already has answers here:
How to read the entire file into a list in python?
(6 answers)
Closed 5 years ago.
I would like to save a file.txt line by line in variable (array or list)
.So if the file.txt is:
hi there
what's up
I want my code to save it line by line in the same variable making a table so I can access easily each line when I want using that variable. And what can I do if I wanna access only line 2? It is supposed I don't know how many lines the file.txt has.
Thank you very much!
Well it is super easy in Python:
text_file = open("filename.txt", "r")
# Splits the element by "\n"
lines = text_file.readlines()
text_file.close()
#Print List of Lines in your File
print(lines)
#Print Number of Lines in your File
print(len(lines))
#Access Second Line in your file
print(lines[1])# Since python is zero indexed

How to read text file line by line and do something for a particular line? [duplicate]

This question already has answers here:
How to jump to a particular line in a huge text file?
(17 answers)
Closed 8 years ago.
I want to read a text file line by line. I found how to read line by line by searching but not how to call a specific line in a text file. Basically, i want to do something with particular lines( like the first line, the second line, the third line, etc):
if particular_line is something:
....
Also, how can i do something like this:
if return_from_another_function in file:
....
Basically, i want an example of how i could do that if it's possible.
f = open('filename', 'r')
lines = f.readlines()
now you get a list type object lines which you can use to access particular line or iterate and search for particular line.
Probably this will help:
myfile = open(filename, "rb", 0)
for line in myfile
if(line is "your string to be compared")
print "do something here"
The standard linecache module makes this a snap:
import linecache
theline = linecache.getline(thefilepath, desired_line_number)
For your second que (from Ans):
If your file is not too large, you can read it into a string, and just use that (easier and often faster than reading and checking line per line):
if 'blabla' in open('example.txt').read():
print "true"

Categories