Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 导入的Python代码工作不正常_Python 2.7 - Fatal编程技术网

Python 2.7 导入的Python代码工作不正常

Python 2.7 导入的Python代码工作不正常,python-2.7,Python 2.7,我正在测试下面的python代码,以便将内容从一个txt文件导出到另一个txt文件,但在目标文件中,内容被复制到了一些不同的语言中,可能是中文,但不正确 # - *- coding: utf- 8 - *- from sys import argv from os.path import exists script,from_file,to_file = argv print "Does the output file exist ? %r" %exists(to_file) print

我正在测试下面的python代码,以便将内容从一个txt文件导出到另一个txt文件,但在目标文件中,内容被复制到了一些不同的语言中,可能是中文,但不正确

# - *- coding: utf- 8 - *-

from sys import argv
from os.path import exists

script,from_file,to_file = argv

print "Does the output file exist ? %r" %exists(to_file)
print "If 'YES' hit Enter to proceed or terminate by CTRL+C "
raw_input('?')

infile=open(from_file)
file1=infile.read()

outfile=open(to_file,'w')
file2=outfile.write(file1)

infile.close()
outfile.close()

请尝试以下代码将1个文件复制到另一个文件:

with open("from_file") as f:
   with open("to_file", "w") as f1:
       for line in f:
            f1.write(line) 

您可以使用python将文件1复制到文件2,而不是使用读/写方法。实际上,我试图在代码中测试读写函数,因此没有使用“复制文件”选项。无论如何,感谢您的建议。我试图测试读写函数,因为我的最终目标是从文本文件中读取数据并将其加载到.csv文件中。我能够找到它,代码是正确的正在工作。谢谢你的建议。