>>> 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 operation1 # 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())