File input and output
File input and output if-then-else
Genome 559: Introduction to Statistical and Computational Genomics
Prof. James H. Thomas
Opening files
? The open() command returns a file object:
= open(, )
? Python will read, write or append to a file according to the access type requested: ? 'r' = read ? 'w' = write ? 'a' = append
? Open for reading a file called "hello.txt":
>>> myFile = open("hello.txt", "r")
Reading the whole file
? You can read the entire content of the file into a single string. If the file content was the text "Hello, world!\n":
>>> myString = myFile.read() >>> print myString Hello, world!
>>>
why is there a blank line here?
Reading the whole file
? Now add a second line to your 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:
>>> 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
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- python functions file reading and writing
- python 2 7 quick reference
- all i ever needed to know about python scripting
- programming principles in python csci 503 490
- python dicts and sets inspiring innovation
- file input and output and conditionals
- lecture 5 python princeton university
- learning the pythonic way
- file input and output
- python programming scripting
Related searches
- input and output calculator
- input and output table calculator
- java input and output stream
- java input and output file
- input and output equations
- input and output function math
- input and output math solver
- input and output equation
- input and output in math
- input and output equation calculator
- input and output tables
- input and output in java