Day 6
Lesson 17
这节课其实就是学了更多更改文件的函数
这个就是把一个文件复制另一个文件
from sys import argv
from os.path import exists
script, from_file, to_file = argv
print("Copying %s to %s" % (from_file, to_file))
in_file = open(from_file)
in_data = in_file.read()
print("The input file is %d byts long!" % len(in_data))
print("Does the output file exist? %r" % exists(to_file))
out_file = open(to_file, 'w')
out_file.write(in_data)
out_file.close()
in_file.close()
Before:
After:
Lesson 19
这课就是讲用不同的方式传参(传参数)
def i_a_c(icecream, chocolate):
print("You have %d boxes of ice-cream!" % icecream)
print("You have %d boxes of chocolate!" % chocolate)
print("Get some marshmallow!\n")
print("We can just give the numbers directly:")
print(i_a_c(20, 35))#这里别忘换行啊!
print("Or we can use variable from our script")
a_icecream = 10
a_chocolate = 15
i_a_c(a_icecream, a_chocolate)
print ("We can do math")
i_a_c(10+5, 20+10)
print ("We can even combine the two of them!")
i_a_c(a_icecream+5, a_chocolate+10)
Lesson 20
好啦 现在可以用自己写的函数来访问文件啦!
from sys import argv
script, input_file = argv
def print_all(f):
print (f.read())
def rewind(f):
f.seek(0)
def print_a_line(line_count,f):
print (line_count, f.readline())
current_file = open(input_file)
print("First let's print the whole file:\n")
print_all(current_file)
print("Now let's rewind, kinda like a tape~")
rewind(current_file)
print("Let's print three lines:")
current_line = 1
print_a_line(current_line,current_file)
current_line = current_line+1
print_a_line(current_line,current_file)
current_line = current_line+1
print_a_line(current_line,current_file)
好啦 今天就到这里 顺便说一句 为什么我写的字比较少 不做过多的解释
是因为由于这也是我第一次学 这本书也比较“神奇” 我不能保证自己在第一次独自理解的完全正确 分享代码只是为了以博客激励自己坚持下来 当然 所有代码多少都有一些自己的改动 希望你们需要的话也能参考~
Au revoir~