Learn the python the hard way (Day 2)

Day 2, I'll introduce raw_input(), it will help us give user some information, and let them to decide what road to go.
>>> name = raw_input("what's your name:")
what's your name:terry
>>> print name
terry

raw_input()

raw_input(...)
    raw_input([prompt]) -> string
    
    Read a string from standard input.  The trailing newline is stripped.
    If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
    On Unix, GNU readline is used if enabled.  The prompt string, if given,
    is printed without a trailing newline before reading.

give some parameter at the beginning, it is very interesting.

1 #parameter, package, variable
  2 
  3 from sys import argv
  4 
  5 script,first,second, third = argv
  6 
  7 print "The script is called:", script
  8 
  9 print "Your first viriable is :", first
 10 
 11 print "Your sencond viriable is :", second
 12 
 13 print "Your third viriable is :", third  
---------------------------------------------------------------
[root@huan python_zed]# python ex13.py a b c
The script is called: ex13.py
Your first viriable is : a
Your sencond viriable is : b
Your third viriable is : c

user have lots of chance to decide which they like,  is it good?

now I'll introduce another useful tool -- file operation
1 # read file 
  2 from sys import argv                     # import argv module
  3 
  4 script,filename = argv                   #  unpack argv to script and filename varible
  5 
  6 txt = open(filename)                     # open a filename and return the file address to txt
  7 
  8 print "Here's your file %r:" % filename  # print filename
  9 
 10 print txt.read()                         # txt get the file and read content. than print them
 11 
 12 txt.close()                              # close file
 13
 14 print "Type the filename again:" # print reminder
 15 
 16 file_again = raw_input(">")    # get file name again by manual
 17 
 18 txt_again = open(file_again)  # open filename by your input 
 19 
 20 print txt_again.readline()       # print the content of your file_again
 21 
 22 txt_again.close()
------------------------------------------
[root@alicia ex15]# python 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 un to have in here.

Type the filename again:
>ex15_sample.txt
This is stuff I typed into a file. 

read and write file to complete copy file feature

[root@huan ex17]# vi ex17.py
  1 # more of file operation
  2
  3 from sys import argv                          # import argv
  4
  5 from os.path import exists # import exists,if filename exist,return true
  6
  7 script, from_file, to_file = argv     #unpack for argv
  8
 10
 11 # we could do these two on one line too,how?
 12
 13 input = open(from_file)      # open from_file
 14
 15 indata= input.read()        # read content of from_file
 16
 17 print "The input file is %d bytes long" % len(indata) # length of from_file
 18
 19 print "Does the output file exist? %r " %exists(to_file) # to_file is exist?
 20
 21 print "Ready, hit RETURN to continue, CTRL-C to abort."
 22
 23 raw_input()          #if CTRL-C everthing is over, the others will continue
 24
 25 output = open(to_file,'w')          # open to_file
 26
 27 output.write(indata)               # write from_file to to_file
 28
 29 print "Alright, all done."
 30
 31 output.close()
 32
 33 input.close()
----------------------------------------------------------
[root@huan ex17]# python ex17.py test.txt copied.txt
Coping from test.txt to copied.txt
The input file is 99 bytes long
Does the output file exist? True
Ready, hit RETURN to continue, CTRL-C to abort.
s
Alright, all done.
[root@huan ex17]# cat copied.txt
This is stuff I typed into a file.

It is really cool stuff.

Lots and lots of un to have in here.
another easy way:

[root@huan ex17]# vi ex17_homework.py
  1 # more of file operation
  2
  3 from sys import argv                          # import argv
  4
  5 from os.path import exists # import exists,if filename exist,return true
  6
  7 script, from_file, to_file = argv
  8
  9 open(to_file,'w').write(open(from_file).read())



ok, these are 10~17 lesson's content, introduce raw_input() and file operation, we can read and write about file and give argv, raw_input some parameter privilige to customer to decide what they like.






转载于:https://my.oschina.net/hding/blog/377422

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值