Download presentation
Presentation is loading. Please wait.
Published byCathrin Gehrig Modified over 6 years ago
1
File input and output Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes from 2009: Sample problem 4, the description differs from the example. First “if-else” slide, must define seq.
2
<filehandle> = open(<filename>, <access type>)
Opening files The open() command returns a file object. <filehandle> = open(<filename>, <access type>) Python can read, write or append to a file: 'r' = read 'w' = write 'a' = append Create a file called “hello.txt” containing one line: “Hello, world!” >>> my_file = open("hello.txt", "r")
3
Why is there a blank line here?
Reading the whole file You can read the contents of the file into a single string. >>> my_string = my_file.read() >>> print my_string Hello, world! >>> Why is there a blank line here?
4
Reading the whole file Now add a second line to your file (“How ya doin’?”) and try again. >>> my_file = open("hello.txt", "r") >>> my_string = my_file.read() >>> print my_string Hello, world! How ya doin'? >>>
5
Reading the whole file Alternatively, you can read the file into a list of strings. >>> my_file = open("hello.txt", "r") >>> my_string_list = my_file.readlines() >>> print my_string_list ['Hello, world!\n', "How ya doin'?\n"] >>> print my_string_list[1] How ya doin'?
6
Reading one line at a time
The readlines() command puts all the lines into a list of strings. The readline() command returns the next line. >>> my_file = open("hello.txt", "r") >>> my_string = my_file.readline() >>> print my_string Hello, world! How ya doin'? >>>
7
Writing to a file Open the file for writing or appending.
>>> my_file = open("new.txt", "w") Use the <file>.write() method. >>> my_file.write("This is a new file\n") >>> my_file.close() >>> ^D > cat new.txt This is a new file Always close a file after you are finished reading from or writing to it.
8
Print vs write <file>.write() does not automatically append an end-of-line character. <file>.write() requires a string as input >>> new_file.write("foo") >>> new_file.write(1) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: argument 1 must be string or read-only character buffer, not int
9
<file> = open(<filename>, r|w|a)
<string> = <file>.read() <string> = <file>.readline() <string list> = <file>.readlines() <file>.write(<string>) <file>.close()
10
Sample problem #1 Write a program read-first-line.py that takes a file name from the command line, opens the file, reads the first line, and prints the result to the screen. > python read-first-line.py hello.txt Hello, world! >
11
Solution #1 import sys filename = sys.argv[1]
my_file = open(filename, "r") first_line = my_file.readline() my_file.close() print first_line
12
Sample problem #2 Modify your program to print the first line without an extra carriage return. > python read-first-line.py hello.txt Hello, world! >
13
Solution #2 import sys filename = sys.argv[1]
my_file = open(filename, "r") first_line = my_file.readline() first_line = first_line[:-1] my_file.close() print first_line
14
Sample problem #3 Write a program add-two-numbers.py that reads one integer from the first line of one file and a second integer from the first line of a second file and then prints their sum. > add-two-numbers.py nine.txt four.txt 9 + 4 = 13 >
15
Solution #3 import sys file_one = open(sys.argv[1], "r")
value_one = int(fileOne.readline()) file_two = open(sys.argv[2], "r") value_two = int(fileTwo.readline()) print value_one, "+", value_two, "=", value_one + value_two
16
Sample problem #4 (optional)
sample.txt: 1 2 3 4 5 Write a program called print-nth-line.py that will extract a specified line from a given file. python print-nth-line.py 2 sample.txt 2
17
Solution #4 import sys lineNumber = int(sys.argv[1]) file_name = sys.argv[2] infile = open(file_name, "r") lines = infile.readlines() print lines[lineNumber - 1]
18
Reading Chapter 14 of Think Python (1st edition) by Allen B. Downey.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.