0 1 2 3 4 5 6 7 8 12345678901234567890123456789012345678901234567890123456789012345678901234567890 (real-life) x------xy------yz------z ATOM 2 CA ALA A 2 21.954 -0.569 111.726 1.00 14.00 C x------xy------yz------z 01234567890123456789012345678901234567890123456789012345678901234567890123456789 (Python) 0 1 2 3 4 5 6 7
print
statement may not be appropriate since it adds
white space and line terminators at its discretioncol1 = 10 col2 = 20 output = "%d\t%d" % (col1, col2)
pickle
,
anydbm
, shelve
, …shelve
Moduleshelve
module lets you treat a file like a dictionary,
except fetching and settings values actually read and write from
a file
shelve
shelve
to store the analysis results in
a file:
markov1_prep.pyshelve
is very
convenient, but not very useful for collaborators unless they
are using your codeshelve
shelve
version, we would not need to
modify the saving/restoring part of the code because
shelve
hides that complexity from us%
is the format operator when operand on
the left is a string, aka "format string"
%
"%d\t%d"
is the format string in
the previous example"%d"
is the format sequence
that specifies what part of the format string
should be replaced with an integer
"%d\t%d"
is a format string
for two integers (the two %d
format
sequences) separated by a tab (the \t
)
"%s"
(string),
"%f"
(floating point number),
"%g"
(compact floating point number),
"%x"
(base-16 integer),
…
>>> data = "this is not an integer" >>> "%d" % data Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: int argument required >>> "%d %d" % (1, 2, 3) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: not all arguments converted during string formatting
format
method
that can be used for formatting
>>> "{:d} {:s}".format(1, str(2)) '1 2' >>> "{:d} {:s}".format(1, 2) Traceback (most recent call last): File "", line 1, in ValueError: Unknown format code 's' for object of type 'int'
{}
parameter but we prefer the
simplicity of the format operator %
f = open('output.txt', 'w') f.write('Hello world\n') print >> f, 'Hello world' # Python 2 print('Hello world', file=f) # Python 3 f.close()
open
function
specifies the "mode", which may be
"r"
(read),
"w"
(write), or
"a"
(append).
"r"
is the default value.write
method of the fileprint
(which is a function in Python 3
and a statement in Python 2)f = open('output.txt', 'w') f.write('Hello world\n') print('Hello world', file=f) # Python 3 print >> f, 'Hello world' # Python 2 f.close()
write
method takes a single string argument
and sends it into the file.
\n
in the data string because write
is very literalprint
may be used to send data to a file
file
argument to specify the target file>>
operator
to specify the target file\n
in the data string because print
automatically terminates the output linef = open('output.txt', 'w') f.write('Hello world\n') print('Hello world', file=f) # Python 3 print >> f, 'Hello world' # Python 2 f.close()
close
,
which tells Python that there will be no more operations
on the file
with
Statementwith open('output.txt', 'w') as f: f.write('Hello world\n') print('Hello world', file=f) # Python 3 print >> f, 'Hello world' # Python 2
with
statement is used for managing
resources such as fileswith
keyword and is released (in this
case, the file is closed) when the with
statement completes (whether normally or with an exception)as
keyword,
and is valid only within the with
statementopen
call, e.g., file does not existfin = open('input.txt', 'rb') fout = open('output.txt', 'wb')
b = fin.read(20) s = b.decode('utf-8') fout.write(s.encode('utf-8')) fout.write(b'Hello world')
/
as the
joining character, even on Windows which
actually uses \
os
and os.path
modules provide
functions for querying and manipulating file names and paths
os.getcwd
- get current working directoryos.listdir
- list names of items in folderos.path.exists
- check if file or folder existsos.path.isdir
- check if name is a folderos.path.join
- combine names into path>>> import os >>> cwd = os.getcwd() >>> print(cwd) /var/tmp/conrad >>> os.listdir(cwd) ['x', 'chimera-build'] >>> cb = os.path.join(cwd, 'chimera-build') >>> print(cb) /var/tmp/conrad/chimera-build >>> os.path.isdir(cb) True >>> os.listdir(cb) ['build', 'foreign', 'install']
string
,
random
, shelve
and os.path
import
statement
import shelve s = shelve.open(path) h1 = s["h1"] h2 = s["h2"] s.close()
module.function
, like
shelve.open
word.py
is in the same folder
as the scripts, wc.py
and freq.py
,
the import
statement will find the word
module>>> import sys, pprint >>> pprint.pprint(sys.path) ['', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Users\\conrad\\AppData\\Roaming\\Python\\Python27\\site-packages', 'C:\\Python27\\lib\\site-packages']
site-packages
is typically where non-standard packages
are installedsite-packages
directorysite-packages
directory
(PEP 370)
import site print(site.getusersitepackages())
site-packages
directories should make them importable with no other
action on your partimport sys sys.path.insert(0, "/Users/conrad/mylib") sys.path.insert(0, "/Users/conrad/swampy")
PYTHONPATH
environment variable
before invoking Python
export PYTHONPATH="/Users/conrad/swampy:/Users/conrad/mylib" python script_name
/Users/conrad/swampy
and /Users/conrad/mylib
will be searched when
Python encounters an import
statement