Reading Normal & Bioinformatics Files (FASTA)
You're currently learning a lecture from the course:
...
Prerequisite Terminologies
In order to have thorough understanding of the main topic, you should have the basic concept of the following terms:
External Libraries
Duration:
Transcription
By:
Hasnat Noor
Introduction:
In Python, there is no need for importing external library to read files, python provides built-in functions for reading files from PC by just providing the absolute path of the file or by providing the relative path.
Python File Reading Methods
file.read(n) – This method reads n number of characters from the file, or if n is blank it reads the entire file.
file.readline(n) – This method reads an entire line from the text file.
Steps:
To read a file in relative path.
Make a new folder and paste the file in it that you want to read.
Open the VS code, make a new python file and save this file in the same folder in which you saved the file that you want to read.
Now start writing a script.
Declare the handler letsRead.
Use the function open("filename"," "read mode(r)") to open the file.
letsRead = open("EONA.fasta","r")
It opens a file with name of "EONA.fast".
Declare the variable myData to store the data.
Now call the read function with the handler and store the value in the declared variable.
myData= letsRead.read()
Call the print function to print the data that is in the EONA.fasta file.
To read a file in absolute path
Save the python and the file that you want to read in different folders/locations.
Open the python file.
Declare the handler letsRead.
Now open the folder in which you pasted the file that you want to read.
Copy its address.
Open the VS code file in which you’re writing the script.
Use the function open("filename"," "read mode(r)") to open the file.
Paste the address in open function at the place of file name.
letsRead = open("C:/Users/twDig/Desktop/EONA.fasta","r")
It opens file with name of "EONA.fast".
Declare the variable myData to store the data.
Now call the read function with the handler and store the value in the declared variable.
myData= letsRead.read()
Call the print function to print the data that is in the EONA.fasta file.
Summary:
In this video we discussed the script to read data from files by providing relative path and absolute path. We also discussed the different function to read the file data.
File(s) Section
If a particular file is required for this video, and was discussed in the lecture, you can download it by clicking the button below.