Tuesday 10 March 2015

filing in python for strings basics

File is basically used to read and write data from a hard drive. To open a file in python we use the open() function. The general syntax of open() function is:
   file_object=open(filename, mode)
  where file_object is the new file object. you can give it any meaning full name like filing, DNAfile ,value etc.
  filename will be the name of a file located anywhere in your computer. If your file is present on the same location where your source code is present,  then you just need to write the name of file. 
If file is present some where else then you need to write the complete path of that file.
  mode is the special characters which tells that what we are going to do with the file. By default it is always in read mode. You can also specify it by using letter r.
Example:
DNA=open("sequence.txt", 'r')
print(DNA)
DNA.(close)

DNA=open("C:\\Users\\Administrator\\Desktop\\sequence.txt", 'r')
print(DNA)

To read an entire text file
DNA=open("C:\\Users\\Administrator\\Desktop\\sequence.txt", 'r')
seq=DNA.read()
print(seq)

No comments:

Post a Comment