Lectures‎ > ‎


Copying Files in Linux/UNIX

  • The 'cp' command can be used to copy a file:
$ cat > foo.py
print 'Hello from Foo'
$ python foo.py
Hello from Foo
$ cp foo.py boo.py
$ python boo.py
Hello from Foo
  • The cp command looks like this:
    • cp <filename> <destination_directory or destination_filename>
  • Examples:
    • cp foo.py boo.py
    • cp foo.py /home/web/cs110/week5
    • cp foo.py ../week6/boo.py

Remotely Logging into a a Machine

  • You can use the 'ssh' command to remotely log into another computer as long as you have an account on the target machine:
    • ssh <machine_name>
  • For example:
    • ssh stargate.cs.usfca.edu
  • You will have access to a shell and all the normal Linux/UNIX commands

Copying Files between two Machines in Linux/UNIX

  • You can also copy files between two different computers
  • Use the 'scp' command (it stands for "secure copy")
  • For example if you want to copy some of you work from your virtual box machine to your CS home directory you can do the following:
    • scp file.py stargate.cs.usfca.edu:
  • You will be asked for you password.
  • This will copy file.py to your home directory.
  • You can also give a destination directory:
    • scp file.py stargate.cs.usfca.edu:cs110/labs/week5
  • This assumes that the directory /home/<username>/cs110/labs/week5 exists

Using the Finder to Copy from Ubuntu in VirtualBox to your CS Account

  • Bring up the Finder (the Home Folder icon on the left side of the desktop)
  • Select 
    • Go -> Location...
  • In the location box, enter
    • ssh://<username>@stargate.cs.usfca.edu
    • e.g., ssh://benson@stargate.cs.usfca.edu
  • Then go to:
    • home/<username>
    • e.g., home/benson
  • Or
    • home/web/<username>
    • e.g., home/web/benson
  • You can use drag and drop to copy/move files.
  • From the command line, you can now use the shell to copy files:
    • cd ~/.gvfs/SFTP<tab>
    • e.g., on my machine I get:
    • cd ~/.gvfs/SFTP\ for\ benson\ on\ stargate.cs.usfca.edu/
  • You can use this directory to copy files in and out of your CS account

Building Strings in Python

  • Many ways to build up strings in Python
  • Printing with commas:
    • print "Hello", "World"
  • Printing with out a newline:
    • print "Hello World",
  • Concatenation:
    • s = "Hello" + "World"
  • Format string:
    • s = "Hello %s" % ("World")
  • String Templates (see below)

String Templates in Python

  • There is another useful way to construct strings: the Python String Template:
$ cat template.py 
from string import Template

t = "First: $fname, Last: $lname"
s = Template(t)
print s.substitute(fname='Greg', lname='Benson')

Program Development

  • Break up problem into smaller pieces
  • Get pieces working first
  • Start to build bigger pieces from smaller ones
  • Incremental development
  • Get partial functionality working fully, then add additional functionality.

Console-based Unit Conversion Tables

  • You should first get your unit conversion functions working with the console.
  • Here is an example:
$ cat in2cm.py
def inches_to_centimeters(inches):
    return inches * 2.54

def centimeters_to_inches(centimeters):
    return centimeters / 2.54
def print_conversion(value1, value2):
    print '%f : %f' % (value1, value2)
if __name__ == '__main__':
    print 'Create inches to centimeters conversion table'
    start = input('Start value: ')
    end   = input('End value  : ')
    step  = input('Step value : ')
    i = start
    while i <= end:
        v1 = i
        v2 = inches_to_centimeters(i)
        print_conversion(v1, v2)
        i = i + step

  • Use if __name__ == '__main__':  to run test code.
  • Show how to turn while loop into a function
  • Show how to select between different conversions

Debugging Programs
  • Much of program development is really debugging.
  • That is, you first write some code that you believe will do what you want.
  • Then you run it and it doesn't work.
    • (a) It may have syntax error, so Python won't even run it.
    • (b) It may have run-time errors (Exceptions).
    • (c) It may not produce the expected output.
  • Case (a), Syntax error are usually easy to figure out, because Python will tell you where the problem is.
  • Case (b) is a little harder because the exception may depend on the input.
    • Also you may not always see run-time problems right away
  • Case (c) requires you to rethink what you are doing.

Debugging CGI Programs

  • Run your script with python on the command line.
  • To send arguments as if they came from the URL and webserver
$ QUERY_STRING="first_name=Greg&last_name=Benson" python cgi.py


  • Compound types and the bracket operator []
    • s = "Hello World"
    • print s[6]
    • c = s[1]
  • Indexing
    • Indicies start at 0
    • print s[0]
    • Can index from end of string with negative numbers: print s[-1]
  • Length (len)
    • Returns length of string
    • len(s)
  • Loop traversal: while
    • We can use our loop template to traverse s string.
  • Loop traversal: for
    • It is easier to use a for loop:

      for c in s:
          print c

  • Slices
    • We can extract a substring from a string using a slice:
    • print s[0:5]
    • Does not include the last index.
    • print s[6:]
  • Comparison
    • Lexigraphical order
    • s2 = "Abracadabra"
    • s2 < s
  • Immutability
    • Can't alter strings: s[0] = "b'' won't work.
    • Instead, you just create a new string
    • s_new = "Bye " + s[6:]
    • print s_new
  • The in operator
    • You can determine if a character is in a string using "in"
    • "e" in s
  • Example: count characters
    • We want to write a function that determine how many times a character occurs in a string:
    • def occurrences(s, c):

Greg Benson,
Sep 24, 2013, 3:48 PM
Greg Benson,
Sep 24, 2013, 3:49 PM