使用Ruby,我可以
File.open('yyy.mp4', 'w') { |f| f.write(File.read('xxx.mp4')}
我可以使用Python吗?
当然可以:
with open('yyy.mp4', 'wb') as f:
f.write(open('xxx.mp4', 'rb').read())
注意,在这里使用了二进制模式标志(b
),因为你正在复制mp4
内容,你不希望Python重新解释换行符。
如果xxx.mp4
很大,那么这将占用大量内存。可以尝试使用shutil.copyfile
函数来获得更节省内存的选项:
import shutil
shutil.copyfile('xxx.mp4', 'yyy.mp4')
import shutil; shutil.copyfile("xxx.mp4","yyy.mp4")
避免这种情况! 我曾经为了加速一个与 Python 完全无关的极特殊的情况而这样做过,但由于在我的 python -c "Put ️ code here"
命令行中存在换行符以及 Meson 处理它的方式。
f.write
移到和with open(...)
同一行,这样它们就都在“同一行”上了 :^) - mgilsonwith open('yyy.txt', 'wb') as f, open('xxx.txt', 'rb') as g:
然后f.write(g.read())
会不会更加简洁一些呢? - martineau