您的位置:首页 > 编程语言 > Python开发

笨办法学Python-习题13-17

2018-01-23 11:11 465 查看
习题13:

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()感觉加不到这一行代码中

但是能实现基本的拷贝功能

上面的代码被我用'''注释掉了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: