Python3: ZipFile instance has no attribute 'extractall' - python-3.x

from zipfile import ZipFile
fzip=ZipFile("crackme.zip")
fzip.extractall(pwd=b"mysecretpassword")
the script works only on IDLE, but when i run it from the command line, it displays:
unzip.py
fzip.extractall(pwd=b"mysecretpassword")
^
SyntaxError: invalid syntax
what's wrong?

It works (Ubuntu 13.04):
>>> import sys
>>> sys.version
'3.3.1 (default, Apr 17 2013, 22:32:14) \n[GCC 4.7.3]'
>>> from zipfile import ZipFile
>>> f = ZipFile('a.zip')
BTW, pwd should be bytes objects:
>>> f.extractall(pwd="mysecretpassword")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.3/zipfile.py", line 1225, in extractall
self.extract(zipinfo, path, pwd)
File "/usr/lib/python3.3/zipfile.py", line 1213, in extract
return self._extract_member(member, path, pwd)
File "/usr/lib/python3.3/zipfile.py", line 1275, in _extract_member
with self.open(member, pwd=pwd) as source, \
File "/usr/lib/python3.3/zipfile.py", line 1114, in open
raise TypeError("pwd: expected bytes, got %s" % type(pwd))
TypeError: pwd: expected bytes, got <class 'str'>
>>> f.extractall(pwd=b'mysecretpassword')
>>>
According to zipfile.ZipFile.extractall documentation:
Warning Never extract archives from untrusted sources without prior inspection. It is possible that files are created outside of
path, e.g. members that have absolute filenames starting with "/" or
filenames with two dots "..".
Changed in version 3.3.1: The zipfile module attempts to prevent that. See extract() note.

Related

ValueError: too many values to unpack (expected 2) in pytube

I'm trying to make a script that downloads videos from youtube, but I keep getting the below error.
>>> from pytube import YouTube
>>> vid = YouTube("https://www.youtube.com/watch?v=dfnCAmr569k")
Traceback (most recent call last):
File "<stdin>", line1, in <module>
File "/home/user/.local/lib/python3.8/site-packages/pytube/__main__.py", line 92 in __init__
self.descramble()
File "/home/user/.local/lib/python3.8/site-packages/pytube/__main__.py", line 140 in descramble
apply_signature(self.player_confing_args, fmt, self.js)
File "/home/user/.local/lib/python3.8/site-packages/pytube/extract.py", line 225 in apply_signature
cipher = Cipher(js=js)
File "/home/user/.local/lib/python3.8/site-packages/pytube/cipher.py", line 31 in __init__ var, _ =
self.transform_plan[0].split(".")
ValueError: too many values to unpack (expected 2)
It's may "cipher.py" file's parsing fnc error. Just copy & paste bug fixed source to your "pytube/cipher.py".
related thread : https://github.com/nficano/pytube/issues/641#issuecomment-665268989

Error when trying to create a file in a path containing parentheses with Python

Can anyone spot why this is failing?
import pathlib
from shlex import quote
path = 'testdir(abc)/subdir(123)'
filename = 'test'
content = \
"""hello
world
"""
pathlib.Path(path).mkdir(mode=0o770, parents=True, exist_ok=True)
md5_filename = quote(str(pathlib.Path(path) / (filename + '.txt')))
with open(md5_filename, 'w') as f:
f.write(content)
I'm getting this traceback
(tools) $ python test_filemake.py
Traceback (most recent call last):
File "test_filemake.py", line 13, in <module>
with open(md5_filename, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: "'testdir(abc)/subdir(123)/test.txt'"
I think I don't understand posix paths well enough to understand what's going on. If I take the parentheses out of the directory names in the path, it works fine. shlex.quote() is adding the extra layer of double quotes, which seems to be breaking things.

Why can't copy the directory into site-packages?

Dual os in my pc, window10 + debian,the module analyse works fine in my debian,i copy it into c:\mydoc\analyse,reboot into win10.
import shutil
shutil.copy('c:\mydoc\analyse','C:\Users\pengsir\AppData\Local\Programs\Python\Python37\Lib\site-packages')
It encounter a problem:
File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
How to copy the directory into my window10's python library?
>>> import shutil
>>> shutil.copy(r'c:\mydoc\analyse',r'C:\Users\pengsir\AppData\Local\Programs\Python\Python37\Lib\site-packages')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\pengsir\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 248, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\pengsir\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
PermissionError: [Errno 13] Permission denied: 'c:\\mydoc\\analyse'
I have run cmd with admin permission.
Escape with \\ --double \.
Use copytree method to copy directory to destination.
shutil.copytree('c:\\mydoc\\analyse','C:\\Users\\pengsir\\AppData\\Local\\Programs\\Python\\Python37\\Lib\\site-packages\\analyse')

Bad password error while extracting a zip from python zipfile

I am trying a straight forward code:
from zipfile import ZipFile
password = '1sS34nConn3ryTh3B3st007?'
zip_file = 'file.zip'
with ZipFile(zip_file) as zf:
zf.extractall(pwd=bytes(password,'utf-8'))
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib64/python3.6/zipfile.py", line 1524, in extractall
self._extract_member(zipinfo, path, pwd)
File "/usr/lib64/python3.6/zipfile.py", line 1577, in _extract_member
with self.open(member, pwd=pwd) as source, \
File "/usr/lib64/python3.6/zipfile.py", line 1446, in open
raise RuntimeError("Bad password for file %r" % name)
RuntimeError: Bad password for file <ZipInfo filename='file.csv' compress_type=99 file_size=272074 compress_size=60230>
It works perfectly fine when I am extracting it on windows using either 7z or winrar. py7zr also gives error.
I've tried to encrypt a file from 7z Ui on Windows with your password.
After I ran this script:
from zipfile import ZipFile
password = '1sS34nConn3ryTh3B3st007?'
zip_file = 'file.zip'
zf = ZipFile(self.archive_name, 'r')
zf.setpassword(bytes(password,"utf-8"))
zf.extractall(path=".")
No issue. Can you try as well?
You need to use pyzipper module instead of zipfile, because of the newer AES-256 encoding.

FileNotFoundError: [Errno 2] File and path verified?

This is the print out in Python 3.5.1 Shell:
import os
os.getcwd()
'C:\\Users\\victoria\\AppData\\Local\\Programs\\Python\\Python35-32'
>>> import os
>>> os.path.abspath('.\\hello')
'C:\\Users\\victoria\\AppData\\Local\\Programs\\Python\\Python35-32\\hello'
>>> helloFile = open('C:\\Users\\victoria\\AppData\\Local\\Programs\\python\\Python35-32\\hello.txt')
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
helloFile = open('C:\\Users\\victoria\\AppData\\Local\\Programs\\python\\Python35-32\\hello.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\victoria\\AppData\\Local\\Programs\\python\\Python35-32\\hello.txt'
What am I reading wrong? I have the .txt file "hello" and it's path verified.
The problem here is that you are trying to open a file that don't exist or you don't put the right name for it. Perhaps you file is actually called hello but when you try to open it you call it as hello.txt which don't exist
Also os.path.abspath don't give you the path to a actual file, it convert a given path into absolute version of it
To check the existence of a file, you must use os.path.exists
example
>>> import os
>>> path=os.path.abspath(".\\fake_file.txt")
>>> path
'C:\\Users\\David\\Documents\\Python Scripts\\fake_file.txt'
>>> os.path.exists(path)
False
>>> path2=os.path.abspath(".\\test.txt")
>>> path2
'C:\\Users\\David\\Documents\\Python Scripts\\test.txt'
>>> os.path.exists(path2)
True
>>>

Resources