2014年2月13日木曜日

PyLadies South Bay and Learn Python the Hard Way 続き 16-18

PyLadies South Bay に参加して Learn Python the Hard Way をちょこっと進めました :)

201402 PyLadies

会場は LinkedIn 本社でした。

Pyladies

Exercise 16: Reading and Writing Files

Commands to remember:

open -- Open the file.
close -- Closes the file.
read -- Reads the contents of the file. You can assign the result to a variable.
readline -- Reads just one line of a text file.
truncate -- Empties the file. Watch out if you care about the file.
write(stuff) -- Writes stuff to the file.

When opening, use:

'w' for write
'r' for read
'a' for append

from sys import argv

script, filename = argv

print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."

raw_input("?")

print "Opening the file..."
target = open(filename, 'w')

# actually not needed since we are doing 'w'...
print "Truncating the file. Goodbye!"
target.truncate()

print "Now I'm going to ask you for three lines."

line1 = raw_input("line 1:")
line2 = raw_input("line 2:")
line3 = raw_input("line 3:")

print "I'm going to write these to the file."

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print "Let's add three more lines."

line4 = raw_input("line 4:")
line5 = raw_input("line 5:")
line6 = raw_input("line 6:")

print "And those will be added to the file."

target.write(line4 + "\n" + line5 + "\n" + line6 + "\n")

print "And finally, we close it."
target.close()


Exercise 17: More Files

cat con*cat*enate, print a file to the screen


from sys import argv
from os.path import exists

script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)

in_file = open(from_file)
indata = in_file.read()

print "The input file is %d bytes long" % len(indata)

print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()

out_file = open(to_file, 'w')
out_file.write(indata)

print "Alright, all done."

out_file.close()
in_file.close()


Exercise to make it shorter:

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)

indata = open(from_file).read()

print "The input file is %d bytes long" % len(indata)

print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()

out_file = open(to_file, 'w')
out_file.write(indata)

print "Alright, all done."

out_file.close()


Exercise 18: Names, Variables, Code, Functions


Functions do 3 things:
-They name pieces of code the way variables name strings and numbers.
-They take arguments the way your scripts take argv.
-Using #1 and #2 they let you make your own "mini-scripts" or "tiny commands."

#scripts with argv
def print_two(*args):
  #unpack argument
  arg1, arg2 = args
  print "arg1: %r, arg2: %r" %(arg1, arg2)

#simplify the above
def print_two_again(arg1, arg2):
  print "arg1: %r, arg2: %r" %(arg1, arg2)

#take one argument
def print_one(arg1):
  print "arg1: %r" % arg1

#take no argument
def print_none():
  print "I got nothin'."

print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()

function checklist

1. Did you start your function definition with def?
2. Does your function name have only characters and _ (underscore) characters?
3. Did you put an open parenthesis ( right after the function name?
4. Did you put your arguments after the parenthesis ( separated by commas)?
5. Did you make each argument unique (meaning no duplicated names)?
6. Did you put a close parenthesis and a colon ): after the arguments?
7. Did you indent all lines of code you want in the function four spaces? No more, no less.
8. Did you "end" your function by going back to writing with no indent (dedenting we call it)?

And when you run ("use" or "call") a function, check these things:

1. Did you call/use/run this function by typing its name?
2. Did you put the ( character after the name to run it?
3. Did you put the values you want into the parenthesis separated by commas?
4. Did you end the function call with a ) character?

Also: to 'run,' 'call,' or 'use' a function all mean the same thing.

過去ログ:
SF Python Hack Night / Learn Python the Hard Way 1-6
Learn Python the Hard Way 続き 7-10

Disclaimer このブログは山崎富美の個人的なものです。ここで述べられていることは私の個人的な意見に基づくものであり、私の雇用者には一切の関係はありません。