from os.path import exists # This returns True if a file 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, how?
in_file = open (from_file)
indata = in_file.read ()
print "The input file is %d bytes long" % len(indata)
print "Does the output file exist? %r" % exists (to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()
out_file = open (to_file, "w")
out_file.write (indata)
print "Alright, all done."
out_file.close () # don't forgot ()!
in_file.close ()
在powershell测试之后出现:
> python "lesson 17.py" "lesson 17s.txt" "lesson 17s2.txt"
Copying from lesson 17s.txt to lesson 17s2.txt
The input file is 21 bytes long
Does the output file exist? False
Ready, hit RETURN to continue, CTRL-C to abort.
Traceback (most recent call last):
File "lesson 17.py", line 16, in