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>
- cp foo.py boo.py
- cp foo.py /home/web/cs110/week5
- cp foo.py ../week6/boo.py
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
Remotely Logging into a a Machine
- You can use the 'ssh' command to remotely log into another computer:
- For example:
- ssh stargate.cs.usfca.edu
- You will have access to a shell and all the normal Linux/UNIX commands
String Templates in Python
- So far, we have looked at three approaches to constructing strings
- Print with commas: print a, "foo", 1
- String concatenation with "+": s = "U" + "S" + "F"
- The string format operator: print "Age: %d" % (21)
- 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')
- 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
return inches * 2.54
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)
i = i + step
- 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