Python自学笔记 Day 6: Lesson 17 & Lesson 19 & Lesson 20

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~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值