笨办法学Python-习题13-17
2018-01-23 11:11
465 查看
习题13:
3.将 raw_input 和 argv 一起使用,让你的脚本从用户手上得到更多的输入
尝试了一下能否通过raw_input来输入命令行参数
结果好像是不行的
不过可以通过raw_input更改命令行参数~
习题14:
简化版代码
习题15:
在同一个文件夹下用相对路径读取失败了,只好把文件直接拖进cmd里
关于文件的相对路径绝对路径可以看看这篇
Python学习:绝对路径和相对路径
windows下查看pydoc的命令行代码:python -m pydoc
习题16:
close – 关闭文件。跟你编辑器的 文件->保存.. 一个意思。
read – 读取文件内容。你可以把结果赋给一个变量。
readline – 读取文本文件中的一行。
truncate – 清空文件,请小心使用该命令。
write(stuff) – 将 stuff 写入文件。
下面是关于加分习题的整合代码
习题17:
但是能实现基本的拷贝功能
上面的代码被我用'''注释掉了
3.将 raw_input 和 argv 一起使用,让你的脚本从用户手上得到更多的输入
尝试了一下能否通过raw_input来输入命令行参数
结果好像是不行的
不过可以通过raw_input更改命令行参数~
习题14:
简化版代码
from sys import argv script,username=argv prompt='~' print "Hi,I'm the %s script." %script print "I know your name,%s" %username print "Do you like me?" likes=raw_input(prompt) print "you really %s like me." %likes
习题15:
在同一个文件夹下用相对路径读取失败了,只好把文件直接拖进cmd里
关于文件的相对路径绝对路径可以看看这篇
Python学习:绝对路径和相对路径
windows下查看pydoc的命令行代码:python -m pydoc
习题16:
close – 关闭文件。跟你编辑器的 文件->保存.. 一个意思。
read – 读取文件内容。你可以把结果赋给一个变量。
readline – 读取文本文件中的一行。
truncate – 清空文件,请小心使用该命令。
write(stuff) – 将 stuff 写入文件。
下面是关于加分习题的整合代码
from sys import argv script,filename=argv target =open(filename,'w') target.truncate() line1=raw_input("line1:") line2=raw_input("line2:") target.write(line1+"\n"+line2) target.close() test=open(filename) print test.read()
习题17:
from sys import argv from os.path import exists script,fromfile,tofile=argv '''print "Copying from %s to %s"%(fromfile,tofile) input=open(fromfile) indata=input.read() print "The input file is %d bytes long"%len(indata) print("Does the output file exists?%s")%exists(tofile) output=open(tofile,'w') output.write(indata) output.close() input.close()''' open(tofile,'w').write(open(fromfile).read())close()感觉加不到这一行代码中
但是能实现基本的拷贝功能
上面的代码被我用'''注释掉了
相关文章推荐
- 笨办法学python加分习题17
- 笨办法学python习题13 参数、解包和变量
- 笨办法学Python-习题6
- 笨办法学Python-习题7,8,9
- 笨办法学Python-习题11
- 【笨办法学Python】习题11:打印出改变了的输入
- 笨办法学python加分习题31
- 笨办法学python加分习题36
- 笨办法学python笔记:习题11 提问(input()函数使用)
- 笨办法学python习题18 命名、变量、代码和函数
- 笨办法学Python-习题37-39
- 笨办法学python加分习题15
- while循环,习题33,笨办法学python
- 笨办法学Python-习题32-36
- 笨办法学python 习题46
- 笨办法学Python笔记二——习题5,8
- 笨方法学python习题13、14
- 【c++习题】【17/4/13】stack
- 笨办法学习python习题3
- 笨办法学python 习题24