Why unzip is not working correctly when called from python with subprocess.Popen?

Hi all, faced with such a problem.
There is a bot which sends a ZIP archive, it saves on my computer, and then have to unpack to another folder, and then have other actions... For this is a bash script which when run from the console, the whole thing works successfully (archive unpacked without errors and everything is OK).
But using the bot, this script is, from python, the whole thing looks like this:
proc = subprocess.Popen('test.sh', stdout=subprocess.PIPE)
 output = proc.stdout.read()
 result = output
 bot.send_message(message.chat.id, result)

Error in logs following:
End-of-central-directory signature not found. Either this file is not
 a zipfile, or it constitutes one disk of a multi-part archive. In the
 the latter case the central directory and zipfile comment will be found on
 the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of (the path to the archive), period.
March 19th 20 at 09:12
2 answers
March 19th 20 at 09:14
Solution
why do you unzip the outside, if you can elegantly pokodit ?
import zipfile
zip_ref = zipfile.ZipFile(path_to_zip_file, 'r')
zip_ref.extractall(directory_to_extract_to)
zip_ref.close()
The fact that this can be either a file or an archive file. In this bash script has tested for it, archive it or not, and then have what to do for each of them.
Can I perform this check in python? - stanford.McDermott99 commented on March 19th 20 at 09:17
of course you can :) - Margarete23 commented on March 19th 20 at 09:20
@Margarete23, [ERROR] - root - File is not a zip file - stanford.McDermott99 commented on March 19th 20 at 09:23
because chekat need

zipfile.is_zipfile(filename)

Returns True if filename is a valid ZIP file based on its magic number, otherwise returns False. the filename may be a file or file-like object too.

Changed in version 3.1: Support for file and file-like objects. - Margarete23 commented on March 19th 20 at 09:26
@Margarete23, then do not understand, I did this:
if os.path.splitext(src)[1] == ".zip":
 zip_ref = zipfile.ZipFile(filename, 'r')
zip_ref.extractall("path")
zip_ref.close()
 bot.send_message(chat_id, "It's a ZIP file")
else:
 bot.send_message(chat_id, "WRONG FILE!!!")

The file is saved, the answer arrives that it is a ZIP file, but the archive is not unpacked. - stanford.McDermott99 commented on March 19th 20 at 09:29
the file extension is not a good idea for detect file type. I would even say very bad
everything else works, checked on their projects, read the documentation - Margarete23 commented on March 19th 20 at 09:32
@Margarete23using zipfile.is_zipfile(filename), for me the answer is that it isn't ZIP, how can this be? And why is this the unpacked files, unzip th via bash and python is not.
XS already how to overcome... - stanford.McDermott99 commented on March 19th 20 at 09:35
well, of course I can do it for You, but only through the exchange of freelancing.
of advice and assistance all the necessary methods of the library and where to look - I pointed out
look, sort it out - Margarete23 commented on March 19th 20 at 09:38
Receive a file from chat so
file_info = bot.get_file(message.document.file_id)
downloaded_file = bot.download_file(file_info.file_path)
src = 'path' + message.document.file_name;
with open(src, 'wb') as new_file:
 new_file.write(downloaded_file)

It and gave an error that it is not the files. Decided very banal, in the end it was necessary to add the closure of the file, in my case, it's a new_file.close() then everything works like a clock. - stanford.McDermott99 commented on March 19th 20 at 09:41
March 19th 20 at 09:16
Thing is, I think, not in Python

Take some files, compute the hash, send the bot calculate the hash of the received files, compare

Find more questions by tags TelegramBotsbashPython