Django TemporaryUploadedFile 不存在,但它被成功读取
Django TemporaryUploadedFile does not exist but nevertheless it is read successfully
我这里有以下情况。我的 OS 显示我通过 POST 请求获得的 django TemporaryUploadedFile
不再存在,但是可以读取这个上传的文件。
这是代码
text_file = request.FILES['text_file']
print(text_file.temporary_file_path())
os.system('ls -l ' + text_file.temporary_file_path())
fs = FileSystemStorage()
file_new =fs.save(text_file.name, text_file)
print(text_file.temporary_file_path())
os.system('ls -l ' + text_file.temporary_file_path())
fs.delete(file_new)
for chunk in text_file.chunks():
text += chunk.decode(encoding)
print('Got text OK.')
这给出了以下输出:
/tmp/tmp0tngal9t.upload foo.txt
-rw------- 1 mine machine 3072889 oct 18 19:29 /tmp/tmp0tngal9t.upload
/tmp/tmp0tngal9t.upload foo.txt
ls: cannot access '/tmp/tmp0tngal9t.upload': No such file or directory
Got text OK.
所以TemporaryUploadedFile
保存到file_new
后就消失了,后来也被删除了。无论如何 text_file
被块成功读取,我从上传的 foo.txt
文件中获取所有文本。怎么可能?如果 text_file
不再存在,text_file.chunks()
从哪里获取数据?
我使用:
python 3.5.2
django 1.10.2
ubuntu 16.04.1
我发现这个问题仍然存在 bare python,所以它与 django 没有特别的关系,因为在这个例子中我刚刚阅读了 text_file
,它在 request.FILES['text_file']
中打开.
我重新问了类似的问题 focusing on python only. It turned out that the problem is not so related with python either, but with Linux/Unix system file management. I quote here the :
Nothing to do with Python. In C, Fortran, or Visual Cobol you'd have
the same behaviour as long as the code gets its handle from open
system call.
On Linux/Unix systems, once a process has a handle on a file, it can
read it, even if the file is deleted. For more details check that
question (I wasn't sure if it was OK to do that, it seems to be)
On Windows you just wouldn't be able to delete the file as long as
it's locked by a process.
我这里有以下情况。我的 OS 显示我通过 POST 请求获得的 django TemporaryUploadedFile
不再存在,但是可以读取这个上传的文件。
这是代码
text_file = request.FILES['text_file']
print(text_file.temporary_file_path())
os.system('ls -l ' + text_file.temporary_file_path())
fs = FileSystemStorage()
file_new =fs.save(text_file.name, text_file)
print(text_file.temporary_file_path())
os.system('ls -l ' + text_file.temporary_file_path())
fs.delete(file_new)
for chunk in text_file.chunks():
text += chunk.decode(encoding)
print('Got text OK.')
这给出了以下输出:
/tmp/tmp0tngal9t.upload foo.txt
-rw------- 1 mine machine 3072889 oct 18 19:29 /tmp/tmp0tngal9t.upload
/tmp/tmp0tngal9t.upload foo.txt
ls: cannot access '/tmp/tmp0tngal9t.upload': No such file or directory
Got text OK.
所以TemporaryUploadedFile
保存到file_new
后就消失了,后来也被删除了。无论如何 text_file
被块成功读取,我从上传的 foo.txt
文件中获取所有文本。怎么可能?如果 text_file
不再存在,text_file.chunks()
从哪里获取数据?
我使用:
python 3.5.2
django 1.10.2
ubuntu 16.04.1
我发现这个问题仍然存在 bare python,所以它与 django 没有特别的关系,因为在这个例子中我刚刚阅读了 text_file
,它在 request.FILES['text_file']
中打开.
我重新问了类似的问题
Nothing to do with Python. In C, Fortran, or Visual Cobol you'd have the same behaviour as long as the code gets its handle from open system call.
On Linux/Unix systems, once a process has a handle on a file, it can read it, even if the file is deleted. For more details check that question (I wasn't sure if it was OK to do that, it seems to be)
On Windows you just wouldn't be able to delete the file as long as it's locked by a process.