IOError:[Errno 2]没有这样的文件或目录:From_file

问题描述:

我正在写我的第一个代码从一个文件复制到另一个,但得到错误作为IOError:[Errno 2]没有这样的文件或目录:。IOError:[Errno 2]没有这样的文件或目录:From_file

Please help me in resolving this error.I would really appreciate your help. 


from sys import argv 
from os.path import exists 
#unmask the argument 
script, from_file, to_file = argv 

print "Copying file from %s to %s" %(from_file, to_file) 
raw_input() 

# open the file 

f = open(from_file) 
# read the file in a object 
infile = f.read() 

print "Source file is %d bytes long" %len(infile) 
print "Does the output file exist ? %r " %exists(to_file) 
print "Read, hit RETURN to continue, CTRL-C to abort." 

raw_input() 
out_file = open ('to_file','w') 
out_file.write(infile) 

print "Closing both the files" 
f.close() 
out_file.close() 
+0

我真的不知道Python,但一方面使用to_file(不含引号),看看它是否存在,然后你有out_file下一=开放('to_file','w')尝试打开它。它应该是一个文字字符串,还是可以删除引号? – Andrew 2014-09-05 14:42:58

+0

我删除了报价,但仍然得到相同的错误.. – 2014-09-05 15:37:16

变化out_file = open ('to_file','w')out_file = open (to_file,'w')即除去报价

+0

我删除了报价,但仍然得到相同的错误。 – 2014-09-05 15:38:00

+0

那么文件真的存在吗?您应该传递文件的完整路径 – 2014-09-05 15:51:40

+0

是Petrov文件存在。我通过了像f = open(r'C:\ Users \ xxx \ from_file')的完整路径 – 2014-09-05 16:00:18