Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.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
Python附加到二进制文件在Unix中工作,而不是在Windows中_Python_Windows_Unix_File Io - Fatal编程技术网

Python附加到二进制文件在Unix中工作,而不是在Windows中

Python附加到二进制文件在Unix中工作,而不是在Windows中,python,windows,unix,file-io,Python,Windows,Unix,File Io,下面的函数接收来自web请求的文件块并将其组合。它在Unix OSX中工作得很好,但在Windows上却不行。具体地说,该文件是汇编的,但是它总是太小,只有几KB。我不知道是什么原因造成的。没有提出任何异常,这一切似乎都是可行的,只是最后的文件并不全在那里。我已经包含了上下文的整个函数,但是我已经标记了似乎不能正常工作的部分。Python 2.7和Windows Server 2008 R2 @view_config(route_name='upload', renderer='json') d

下面的函数接收来自web请求的文件块并将其组合。它在Unix OSX中工作得很好,但在Windows上却不行。具体地说,该文件是汇编的,但是它总是太小,只有几KB。我不知道是什么原因造成的。没有提出任何异常,这一切似乎都是可行的,只是最后的文件并不全在那里。我已经包含了上下文的整个函数,但是我已经标记了似乎不能正常工作的部分。Python 2.7和Windows Server 2008 R2

@view_config(route_name='upload', renderer='json')
def upload(request):
    r = request.response

    final_dir = 'w:\\foobar'
    filename = request.params.get('flowFilename')
    chunk_number = request.params.get('flowChunkNumber')
    total_chunks = request.params.get('flowTotalChunks')
    try:
        temp_dir = os.path.join(final_dir, request.params.get('flowIdentifier'))
        file_part = os.path.join(temp_dir, '%s.part.%s' % (filename, chunk_number))
        if not os.path.exists(temp_dir):
            os.makedirs(temp_dir)
    except TypeError:
        pass

    if request.method == 'GET':
        if file_part:
            if os.path.isfile(file_part):
                r.status = 200
            else:
                r.status = 404
        return r
    if request.POST:
        try:
            fo = request.params.get('file').file
            f = open(file_part, 'wb')
            f.write(fo.read())
            f.close()
            if chunk_number == total_chunks:
                final_filename = os.path.join(final_dir, filename)
                temp_filename = filename + '_INCOMPLETE'
                #####################################################################
                # This is where is appears to be going wrong...
                final_file = open(temp_filename, 'a+b')
                try:
                    for i in range(1, int(total_chunks) + 1):

                        ff = open(os.path.join(temp_dir, '%s.part.%s' % (filename, i)))
                        final_file.write(ff.read())
                        ff.close()
                    final_file.close()
                    os.rename(temp_filename, final_filename)  # rename to final filename
                    shutil.rmtree(temp_dir)  # clean up temp part files
                except:
                    raise
                ####################################################################
            r.status = 200
        except Exception, e:
            print 'ERROR', e.message
            r.status = 404
        return r

尝试单独保存块,然后使用一些外部工具将它们连接起来。也许在ff=open中。。。行您还需要使用二进制模式?如果这是Python2.x,正如所解释的,模式意味着每次写入都会到达文件末尾,而不管在某些*位上的查找位置如何,但在其他位上和Windows上则不会。此外,您的尝试:/except:raise应该做什么?这只会捕获任何异常并立即重新引用它,这应该不会有任何影响。@abarnert:不清楚这在这里有什么关系,因为他似乎什么都不做,只是打开文件,写入它,然后关闭它。找不到了。