Python将文件读入内存以进行重复的FTP复制 [英] Python read file into memory for repeated FTP copy

查看:142
本文介绍了Python将文件读入内存以进行重复的FTP复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要读取本地文件并通过FTP复制到远程位置,我将同一文件file.txt重复复制了数百次,使用不同的名称(例如f1.txt,f2.txt ... f1000.txt等)复制到远程位置现在,是否有必要始终为每个FTP副本打开,读取,关闭我的本地file.txt,或者是否有办法存储到变量中并一直使用该变量并避免文件打开,关闭功能. file.txt是6KB的小文件.以下是我正在使用的代码

I need to read a local file and copy to remote location with FTP, I copy same file file.txt to remote location repeatedly hundreds of times with different names like f1.txt, f2.txt... f1000.txt etc. Now, is it necessary to always open, read, close my local file.txt for every single FTP copy or is there a way to store into a variable and use that all time and avoid file open, close functions. file.txt is small file of 6KB. Below is the code I am using

for i in range(1,101):
    fname = 'file'+ str(i) +'.txt'
    fp = open('file.txt', 'rb')
    ftp.storbinary('STOR ' + fname, fp)
    fp.close()

我尝试读取一个字符串变量并替换为fp,但ftp.storbinary需要第二个参数具有read()方法,请建议是否有避免文件打开关闭的更好方法,或者让我知道它是否没有性能改善全部.我在Windows 7上使用的是python 2.7.10.

I tried reading into a string variable and replace fp but ftp.storbinary requires second argument to have method read(), please suggest if there is better way to avoid file open close or let me know if it has no performance improvement at all. I am using python 2.7.10 on Windows 7.

推荐答案

只需在循环之前将其打开,并在循环之后将其关闭:

Simply open it before the loop, and close it after:

fp = open('file.txt', 'rb')

for i in range(1,101):
    fname = 'file'+ str(i) +'.txt'
    fp.seek(0)
    ftp.storbinary('STOR ' + fname, fp)

fp.close()  

更新请确保在对ftp.storbinary的调用之前添加fp.seek(0),否则,如@eryksun所述,read调用将在第一次迭代中耗尽文件.

Update Make sure you add fp.seek(0) before the call to ftp.storbinary, otherwise the read call will exhaust the file in the first iteration as noted by @eryksun.

更新2 取决于文件的大小,使用BytesIO可能会更快.这样,文件内容将保存在内存中,但仍然是类似文件的对象(即,它将具有read方法).

Update 2 depending on the size of the file it will probably be faster to use BytesIO. This way the file content is saved in memory but will still be a file-like object (ie it will have a read method).

from io import BytesIO

with open('file.txt', 'rb') as f:
    output = BytesIO()
    output.write(f.read())

for i in range(1, 101):
    fname = 'file' + str(i) + '.txt'
    output.seek(0)
    ftp.storbinary('STOR ' + fname, fp)

这篇关于Python将文件读入内存以进行重复的FTP复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆