Get File path from binary data

2019-05-28 12:10发布

问题:

Is it possible to get the path of a file from binary data?

I have only binary data which came from reading the file, but have no information of file path.

Does the binary data have the information of path? if yes then How can I get it.

I need to read xlsx file using python xlrd lib, which requires the file path, but i only have binary data.

Can anyone shed light on it?

回答1:

I have Done this before like,

def read_file(self, cr, uid, ids, context=None):
    if not context: context = {}
    rec = self.browse(cr, uid, ids[0], context)
    file_path = tempfile.gettempdir()+'/file.xlsx'
    data = rec.file
    f = open(file_path,'wb')
    f.write(data.decode('base64'))
    f.close()
    wb = xlrd.open_workbook(file_path)
    ....

Then you can do your process.



回答2:

Generally there's no way to retrieve the path of a file just from it's content. There may be file formats for which this is possible, but in the general case there is no way.

However, if you need the file's path only to feed the file into some other part of your program, there is another way: Just dump your binary data into any file (or temp file), and use that file's path.



回答3:

If you are getting the binary data from a stream, then there is NO way of getting the file path.

If you think about it, the data may also be coming through stdin or through a network socket, so no file path is associated with it.

A possible approach is to manipulate the program to attach file path information along with the binary stream. We can help more in this approach, if you can post the code of your program.



回答4:

I dont think what you are doing to right. IfIf you have the binary data in your database, then create a buffer using the python StringIO package, then load the binary file to the buffer. Then do whatever you want