Docsity

Python File Input and Output with Conditional Statements, Study Guides, Projects, Research of Genomics

Partial preview of the text

Download Python File Input and Output with Conditional Statements and more Study Guides, Projects, Research Genomics in PDF only on Docsity!

File input and output

and conditionals

Genome 559: Introduction to

Statistical and Computational Genomics

Prof. James H. Thomas

Opening files

  • The built-in open() function returns a file object: <file_object> = open(, )
  • Python will read, write or append to a file according to the access type requested: - 'r' = read - 'w' = write (will replace the file if it exists) - 'a' = append (appends to an existing file)
  • For example, open for reading a file called "hello.txt":

myFile = open('hello.txt', 'r') (NB – this opens a file in the present working directory, you can also provide a full path)

Reading the whole file

  • Now add a second line to the file (“How ya

doin’?\n”) and try again.

myFile = open('hello.txt', 'r') myString = myFile.read() print myString Hello, world! How ya doin'?

Reading the whole file

  • Alternatively, you can read the file into a list

of strings, one string for each line:

myFile = open('hello.txt', 'r') myStringList = myFile.readlines() print myStringList ['Hello, world!\n', 'How ya doin'?\n'] print myStringList[1] How ya doin'? this file method returns a list of strings, one for each line in the file notice that each line has the newline character at the end

Writing to a file

  • Open a file for writing (or appending):

myFile = open('new.txt', 'w') # (or 'a')

  • Use the .write() method:

myFile.write('This is a new file\n') myFile.close() Ctl-D (exit the python interpreter) cat new.txt This is a new file always close a file after you are finished reading from or writing to it. open('new.txt', 'w') will overwrite an existing file (or create a new one) open('new.txt', 'a') will append to an existing file

.write() is a little different from print

  • .write() does not automatically

append a new-line character.

  • .write() requires a string as input.

newFile.write('foo') newFile.write(1) Traceback (most recent call last): File "", line 1, in? TypeError: argument 1 must be string or read-only character buffer, not int newFile.write(str(1)) # str converts to string (also of course print goes to the screen and .write() goes to a file)

The if statement

if (seq.startswith("C")): ... print "Starts with C" ... Starts with C

  • A block is a group of lines of code that belong together. if ():
  • In the Python interpreter, the ellipsis … indicates that you are inside a block (on my Win machine it is just a blank indentation).
  • Python uses indentation to keep track of code blocks.
  • You can use any number of spaces to indent a block, but you must be consistent. Using one is simplest.
  • An unindented or blank line indicates the end of a block.

The if statement

  • Try doing an if statement without indentation:

if (seq.startswith("C")): ... print "Starts with C" File "", line 2 print "Starts with C" ^ IndentationError: expected an indented block the interpreter expects you to be inside a code block (…) .write() does not automatically ### append a new-line character. - .write() requires a string as input. >>> newFile.write('foo') >>> newFile.write(1) Traceback (most recent call last): File "", line 1, in? TypeError: argument 1 must be string or read-only character buffer, not int >>> newFile.write(str(1)) # str converts to string (also of course print goes to the screen and .write() goes to a file) ## The if statement >>> if (seq.startswith("C")): ... print "Starts with C" ... Starts with C >>> - A block is a group of lines of code that belong together. if (): - In the Python interpreter, the ellipsis … indicates that you are inside a block (on my Win machine it is just a blank indentation). - Python uses indentation to keep track of code blocks. - You can use any number of spaces to indent a block, but you must be consistent. Using one is simplest. - An unindented or blank line indicates the end of a block. ## The if statement - Try doing an if statement without indentation: >>> if (seq.startswith("C")): ... print "Starts with C" File "", line 2 print "Starts with C" ^ IndentationError: expected an indented block the interpreter expects you to be inside a code block (…) but it is not indented properly

Comparison and logic operators

  • Boolean: and, or, not
  • Numeric: < , > , ==, !=, >=, <=
  • String: in, not in < is less than

    is greater than == is equal to != is NOT equal to <= is less than or equal to = is greater than or equal to

Examples

seq = 'CAGGT'

if ('C' == seq[0]): ... print 'C is first in', seq ... C is first in CAGGT if ('CA' in seq): ... print 'CA is found in', seq if (seq.startswith("C")): ... print "Starts with C" ... Starts with C >>> - A block is a group of lines of code that belong together. if (): - In the Python interpreter, the ellipsis … indicates that you are inside a block (on my Win machine it is just a blank indentation). - Python uses indentation to keep track of code blocks. - You can use any number of spaces to indent a block, but you must be consistent. Using one is simplest. - An unindented or blank line indicates the end of a block. ## The if statement - Try doing an if statement without indentation: >>> if (seq.startswith("C")): ... print "Starts with C" File "", line 2 print "Starts with C" ^ IndentationError: expected an indented block the interpreter expects you to be inside a code block (…) but it is not indented properly Comparison and logic operators - Boolean: and, or, not - Numeric: < , > , ==, !=, >=, <= - String: in, not in < is less than > is greater than == is equal to != is NOT equal to <= is less than or equal to >= is greater than or equal to ## Examples seq = 'CAGGT' >>> if ('C' == seq[0]): ... print 'C is first in', seq ... C is first in CAGGT >>> if ('CA' in seq): ... print 'CA is found in', seq ... CA is found in CAGGT if (('CA' in seq) and ('CG' in seq)): ... print "Both there!" ...

comparison operators the outer parenthesis pair is optional

Combining tests

x = 1 y = 2 z = 3 if ((x < y) and (y != z)): do something if ((x > y) or (y == z)): do something else Evaluation starts with the innermost parentheses and works out. When there are multiple parentheses at the same level, evaluation starts at the left and moves right. The statements can be arbitrarily complex. if (((x <= y) and (x < z)) or ((x == y) and not (x == z)))

if-else statements

if : else:

  • The else block executes only if is false.

if (seq.startswith('T')): ... print 'T start' ... else: ... print 'starts with', seq[0] ... starts with C

evaluates to FALSE 

Example

base = 'C' if (base == 'A'): ... print "adenine" ... elif (base == 'C'): ... print "cytosine" ... elif (base == 'G'): ... print "guanine" ... elif (base == 'T'): ... print "thymine" ... else: ... print "Invalid base!" ... cytosine

= open(, 'r'|'w'|'a')

= .read()

= .readline()

= .readlines()

.write()

.close()

if : <statement(s)> elif : <statement(s)> else: <statement(s)>

  • Boolean: and, or, not
  • Numeric: < , > , ==, !=, >=, <=
  • String: in, not in