Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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
Can';t在windows上用python复制文件内容_Python_File - Fatal编程技术网

Can';t在windows上用python复制文件内容

Can';t在windows上用python复制文件内容,python,file,Python,File,我正在尝试将文件内容从一个文件复制到另一个文件: srcp = './output/name.jar' dstp = './output/name' os.remove(dstp) src = open(srcp, 'r') dst = open(dstp, 'w+b') shutil.copyfileobj(src, dst) src.close() dst.close() print os.path.getsize(srcp) print os.path.getsize(dstp) 结

我正在尝试将文件内容从一个文件复制到另一个文件:

srcp = './output/name.jar'
dstp = './output/name'
os.remove(dstp)
src = open(srcp, 'r')
dst = open(dstp, 'w+b')
shutil.copyfileobj(src, dst)

src.close()
dst.close()

print os.path.getsize(srcp)
print os.path.getsize(dstp)
结果是:

213815
3896
当我在
shutil.copyfileobj
copid size中指定length参数时,copid大小会发生变化,但也会出错,每个
length
的剪切常数都会发生变化

shutil.copyfile
工作正常,但我需要复制内容,因为我的代码在文件内容之前复制另一个信息。在这个测试用例中,我只是试图复制


系统:win 7 x32

您有不同的文件模式

src = open(srcp, 'r')
dst = open(dstp, 'w+b')

阅读时应使用
'rb'
。否则,换行符将被错误处理,0x1A将被识别为EOF。

为什么使用二进制模式进行写入,而使用“文本模式”进行读取?@Gandaro为什么使用注释模式进行回答?所以我现在得到了你的分数:-)