keywords:
Bookmark and Share



Front Back
d or i
Decimal integer
u
Unsigned integer
o
Octal integer
h or H
Hexadecimal integer
f
Floating-point number
e or E
Floating-point number, exponential form
g or G
Floating-point number, "optional" representation
s
String value -- also provides default representation for any object
c
Single character
%
Literal percent sign
the locals function
returns a dictionary whose keys are the names of all the currently defined variables and whose values are the values of those variables
By default, the print statement terminates its output with...
a newline
'r'
Open file for reading; the file must exist
'w'
Open file for writing; the file will be created if it doesn't exist
'a'
Open file for appending; the file will be created if it doesn't exist
the open function
crates a file object
'r+'
Open file for reading and writing; if the file exists, its content is not destroyed
'w+'
Open file for reading and writing; if the file exists, its content is overwritten
'a+'
Similar to 'r+'
arguments for buffering

  • A value of 0 means no buffering. (Anything written to the file will immediately appear in the file.)
  • A value of 1 means line buffering. (Information is written to the file only when a newline character is sent to it.)
  • Any other non-zero value is the size of the buffer that will be used when transferring information to the file.
IOError exception
raised when any problems are encountered opening a file
2 arguments the open function takes

  • The first argument is the name of the file.
  • The second argument is a string describing the action(s) you wish to perform with the file. The possible values for the second argument are listed in the table below.
readline
reads one line from a file
readlines
reads entire file; returns list of elements
read
reads entire file; returns scalar string
the readline method
will read one line from the specified file. Repeated calls to the readline method will continue to read one line at a time, until the entire contents of the file have been read
the write method
a file object must be created with a second argument of 'w''a''r+', or 'a+', then this method will take its single scalar argument and write it to the file represented by the file object.
the writelines method
accepts a single list as an argument and writes each element of the list as if a call were made to the write method with that element. As with the write method, newlines are not automatically added to the output.
the close method
accepts no arguments, to close a file after you are done reading from or writing to it
the flush method
useful because it ensures that a file's buffers are emptied
the tell method
returns the current offset of a file object in bytes -- in other words, the location in the file where the next operation (read or write) will take place
the seek method
subsequent reads or writes will start at the location in the file that you specified in the call
the truncate method
takes a single integer argument and changes the length of the file represented by the file object to the specified argument in bytes
the name attribute
is either the name of the file from which the file object was created or a string describing the object's source if it wasn't a file
the closed attribute
is an integer variable that is 1 if the file is closed and 0 if the file is open
the mode attribute
is a variable containing the mode with which a file object was originally opened
3 output streams
  • standard input (usually the keyboard)
  • standard output (usually the computer screen)
  • standard error (also usually the computer screen, but a separate stream from the standard output)
stdin
standard input (usually the keyboard)
stdout
standard output (usually the computer screen)
stderr
standard error (also usually the computer screen, but a separate stream from the standard output)
a pipe
behaves just like a file, but it represents either the standard output from some program or the standard input to some program
from os import popen
imports the popen function
import os
imports the entire contents of the os module
x of y cards