刚做一道题:写一个复制文件的程序。如下:
from sys import argv
from os.path import exists
script,from_file,to_file = argv
in_file = open(from_file)
indata = open(from_file).read()
out_file = open(to_file,'w')
out_file.write(indata)
print "Alright,all done"
in_file.close()
out_file.close()
这个程序正常运行,之后题目要求尽量缩短,我想减少变量,改成这样:
out_file.write(open(from_file).read())
open(from_file).close()
open(to_file,'w').close()
程序还是能正常运行。但我改成这样后,发现虽然能运行但无法复制去另一个文件,里面是空白。如下:
open(to_file,'w').write(open(from_file).read())
虽然有些钻牛角尖,但我想知道为什么
Sign in to make a reply
O-_-O-_-O
刚做一道题:写一个复制文件的程序。如下:
from sys import argv
from os.path import exists
script,from_file,to_file = argv
in_file = open(from_file)
indata = open(from_file).read()
out_file = open(to_file,'w')
out_file.write(indata)
print "Alright,all done"
in_file.close()
out_file.close()
这个程序正常运行,之后题目要求尽量缩短,我想减少变量,改成这样:
from sys import argv
from os.path import exists
script,from_file,to_file = argv
in_file = open(from_file)
indata = open(from_file).read()
out_file = open(to_file,'w')
out_file.write(open(from_file).read())
print "Alright,all done"
open(from_file).close()
open(to_file,'w').close()
程序还是能正常运行。但我改成这样后,发现虽然能运行但无法复制去另一个文件,里面是空白。如下:
from sys import argv
from os.path import exists
script,from_file,to_file = argv
in_file = open(from_file)
indata = open(from_file).read()
out_file = open(to_file,'w')
open(to_file,'w').write(open(from_file).read())
print "Alright,all done"
open(from_file).close()
open(to_file,'w').close()
虽然有些钻牛角尖,但我想知道为什么