我有以下方法将a中的所有文件复制directory_path
到django模型中。好了,这可以按预期工作:将文件复制到模型中定义的位置upload_to
。但是有一个问题:不是所有内容都被复制。仅一部分文件。如17.530字节,而不是〜1-2兆字节。:(这让我非常难过。
def _handle_directory(self, directory_path, directory):
for fs_name in os.listdir(directory_path):
fs_path = os.path.join(directory_path, fs_name)
if os.path.isfile(fs_path):
path = os.path.join(directory_path, fs_path)
with open(path, 'r') as f:
file_wrapper = File(f)
self.cnt_files += 1
new_file = FsFile(directory=directory,
filename=fs_name.decode(self.fs_encoding).encode('utf-8'),
file=file_wrapper, uploader=self.uploader)
new_file.save()
更新1:
我用三个文件运行了此文件:
filename | orig. size | imported size | ratio
foo.pdf | 70.818 | 1365 | 1,92 %
bar.html | 3.355 | 3355 | 100 %
fiz.zip | 645 | 135 | 20,93 %
好吧,我回答了这个问题,因为jensq没有。
我必须将b
-flag添加到open
:
with open(path, 'rb') as f:
然后一切都按我预期的方式工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句