ex13
代码
from sys import argv#从系统中输入参数
script, first, second, third = argv
print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)
总结:
后来yaung下午截图,给我讲解14,我又重新做了13,做出来了,感谢!
ex14
#ex14 提示换个传递的代码练习
from sys import argv
script, user_name = argv
prompt = '> '
print("Hi %s, I'm the %s script." % (user_name, script))
print("I'd like to ask you a few questions.")
print("Do you like me %s?" % user_name)
likes = input(prompt)
print("Where do you live %s?" % user_name)
lives = input(prompt)
print("What kind of computer do you have?")
computer = input(prompt)
print("""
Alright, so you said %r about liking me.
You liking in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives, computer))
运行结果
"C:\Program Files\Python36\python.exe" "D:/小克学习/python/项目/week two/ex14.py" Angle
Hi Angle, I'm the D:/小克学习/python/项目/week two/ex14.py script.
I'd like to ask you a few questions.
Do you like me Angle?
> like
Where do you live Angle?
> China
What kind of computer do you have?
> apple
Alright, so you said 'like' about liking me.
You liking in 'China'. Not sure where that is.
And you have a 'apple' computer. Nice.
Process finished with exit code 0
这几章的联系,总是有很多困难,感谢老师的指导。
总结:
1.在python 3. x 版本中,input()的输入格式发生了变化,这个得记住。
2.input是操作键盘的,我们得输入,才执行下面代码(左侧的红色区域就是提示)
ex15
# ex15 读取文件的练习
from sys import argv#解包,和前面的一样
script, filename = argv
txt = open(filename)
print("Here's your file %r:" % filename)
print(txt.read ())
print("Type the filename again:")
file_again = input()
txt_again = open(file_again)
print(txt_again.read())
运行结果
"C:\Program Files\Python36\python.exe" "D:/小克学习/python/项目/week two/ex15.py" ex15_sample.txt
Here's your file 'ex15_sample.txt':
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
Type the filename again:
ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
Process finished with exit code 0
总结:
1.这次熟悉了很多,虽然还是有运行失误,但是自己能按照逻辑进行修改了。
2.txt文件,需要放在同一个路径中去。
python:open/文件操作
open/文件操作f=open('/tmp/hello','w')#open(路径+文件名,读写模式)#读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式如:'rb','wb','r+b'等等
读写模式的类型有:
rU 或 Ua 以读方式打开, 同时提供通用换行符支持 (PEP 278)w 以写方式打开,a 以追加模式打开 (从 EOF 开始, 必要时创建新文件)r+ 以读写模式打开w+ 以读写模式打开 (参见 w )a+ 以读写模式打开 (参见 a )rb 以二进制读模式打开wb 以二进制写模式打开 (参见 w )ab 以二进制追加模式打开 (参见 a )rb+ 以二进制读写模式打开 (参见 r+ )wb+ 以二进制读写模式打开 (参见 w+ )ab+ 以二进制读写模式打开 (参见 a+ )
注意:
1、使用'W',文件若存在,首先要清空,然后(重新)创建,
2、使用'a'模式 ,把所有要写入文件的数据都追加到文件的末尾,即使你使用了seek()指向文件的其他地方,如果文件不存在,将自动被创建。
ex16
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 RETURE.")
input("?")
print("Opening the file...")
target = open(filename, 'w')
print("Truncating the file. Goodbey!")
target.truncate()
print("Now I'm going to ask you for three lines.")
line1 = input("line 1: ")
line2 = input("line 2: ")
line3 = input("line 3: ")
print("I'm going to write these to the file.")
target.write("%s \n%s \n%s" % (line1, line2, line3))
print("And finally, we close it.")
target.close()
运行结果
"C:\Program Files\Python36\python.exe" "D:/小克学习/python/项目/week two/ex16.py" ex15_sample.txt
We're going to erase 'ex15_sample.txt'.
If you don't want that, hit CTRL_C (^C).
If you do want that, hit RETURE.
?
Opening the file...
Truncating the file. Goodbey!
Now I'm going to ask you for three lines.
line 1:
line 2:
line 3:
I'm going to write these to the file.
And finally, we close it.
Process finished with exit code 0
总结:
在代码中的,重复地方,直接用了新的格式
target.write("%s \n%s \n%s" % (line1, line2, line3))
这种写法简洁很多。
想问下老师为什么总是在print下面写 target.write(),
有模糊的感觉,但是抓不住。
ex17
#ex17的练习代码-更多的文件操作
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))
#We could do these two on one line too, how?
in_file = open(from_file)
indata = in_file.read()
print("The input file is %d bytes long" % len(indata))
print("Dose the output file exist? %r" % exists(to_file))
print("Ready, hit RETURE to continue, CTRL_C to abort.")
input()
out_file = open(to_file, 'w')
out_file.write(indata)
print("Alright, all done. ")
out_file.close()
in_file.close()
运行结果
"C:\Program Files\Python36\python.exe" "D:/小克学习/python/项目/week two/ex17.py" test.txt copied.txt
Copying from test.txt to copied.txt
The input file is 0 bytes long
Dose the output file exist? True
Ready, hit RETURE to continue, CTRL_C to abort.
cat copied.tx
Alright, all done.
Process finished with exit code 0
该命令对于任何文件都应该是有效的。试试操作一些别的文件看看结果。不过小心别把你的重要文件给弄坏了。
Warning你看到我用 cat 这个命令了吧?它只能在 Linux 和 OSX 下面使用,使用 Windows 的就只好跟你说声抱歉了。
做完之后发现不对劲,出现了这句话
总结:
越写越懵逼,最好需要在晚上积累沉淀下。