Why can't copy the directory into site-packages? - python-3.x

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')

Related

Python: PermissionError: [Errno 13] with copy_tree function in windows

Im just copying all files and subfolders from an specific directory using the function distutils.dir_util.copy_tree in Windows:
from distutils.dir_util import copy_tree
from datetime import datetime
directories_to_backup = {
'folder_a' : 'e:\\folder_a',
}
now = datetime.now()
backup_name_folder = now.strftime("%Y%m%d-%H%M")
for name_folder, path_folder in directories_to_backup.items():
print(f'Backuping up the folder {name_folder}')
backup_full_path = f't:\\zzzBackup\\{backup_name_folder}\\{name_folder}\\'
copy_tree(path_folder, backup_full_path)
it works fine till it get to some specific file and rise the following error:
Traceback (most recent call last):
File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\file_util.py", line 48, in _copy_file_contents
buf = fsrc.read(buffer_size)
PermissionError: [Errno 13] Permission denied
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/UserR/PycharmProjects/my_scripts/main_backup.py", line 28, in <module>
copy_tree(path_folder, backup_full_path)
File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\dir_util.py", line 159, in copy_tree
verbose=verbose, dry_run=dry_run))
File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\dir_util.py", line 159, in copy_tree
verbose=verbose, dry_run=dry_run))
File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\dir_util.py", line 159, in copy_tree
verbose=verbose, dry_run=dry_run))
[Previous line repeated 1 more times]
File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\dir_util.py", line 163, in copy_tree
dry_run=dry_run)
File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\file_util.py", line 151, in copy_file
_copy_file_contents(src, dst)
File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\file_util.py", line 51, in _copy_file_contents
"could not read from '%s': %s" % (src, e.strerror))
distutils.errors.DistutilsFileError: could not read from 'e:\folder_a\0\binlog': Permission denied
Process finished with exit code 1
How can I update the permissions in windows so my script would be able to read and copy the files?

file not found error, though file is in proper place

C:\Users\ebena\PycharmProjects\untitled2\venv\Scripts\python.exe C:/Users/ebena/PycharmProjects/untitled2/trial.py
Traceback (most recent call last):
File "C:/Users/ebena/PycharmProjects/untitled2/trial.py", line 5, in <module>
image = Image.open("book background.jpeg")
File "C:\Users\ebena\PycharmProjects\untitled2\venv\lib\site-packages\PIL\Image.py", line 2878, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'book background.jpeg'
python cannot access these files and says they do not exist or file not found
Change the name of the file to book_background.jpeg

Zipfile / shutil.make_archive throws EncodeError on german umlauts

I'm trying to zip a folder in Python 3 with the module zipfile.
Since I'm german I have some filenames containing umlauts (äöü).
While zipping, I get a UnicodeEncodeError: 'utf-8' codec can't encode character '\udcfc' in position 95: surrogates not allowed.
The character in question is an ü.
How can I get zipfile to zip all my files?
The relevant code is this:
def zipdir(path, ziph):
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file))
if __name__ == '__main__':
zipf = zipfile.ZipFile('path/to/destination', 'w', zipfile.ZIP_DEFLATED)
zipdir('path/to/folder', zipf)
zipf.close()
Edit:
I've got the same error when I'm using shutil.make_archive.
import shutil
shutil.make_archive('/path/to/destination', 'zip', '/path/to/folder')
Full stacktrace of shutil.make_archive():
Traceback (most recent call last):
File "/usr/lib64/python3.7/zipfile.py", line 452, in _encodeFilenameFlags
return self.filename.encode('ascii'), self.flag_bits
UnicodeEncodeError: 'ascii' codec can't encode character '\udcfc' in position 59: ordinal not in range(128)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "run.py", line 39, in <module>
archive_dir(path, zip_fullpath)
File "run.py", line 19, in archive_dir
shutil.make_archive(dest, 'zip', source)
File "/home/sean/.local/share/virtualenvs/backup-script-QUcRKrDQ/lib/python3.7/shutil.py", line 822, in make_archive
filename = func(base_name, base_dir, **kwargs)
File "/home/sean/.local/share/virtualenvs/backup-script-QUcRKrDQ/lib/python3.7/shutil.py", line 720, in _make_zipfile
zf.write(path, path)
File "/usr/lib64/python3.7/zipfile.py", line 1746, in write
with open(filename, "rb") as src, self.open(zinfo, 'w') as dest:
File "/usr/lib64/python3.7/zipfile.py", line 1473, in open
return self._open_to_write(zinfo, force_zip64=force_zip64)
File "/usr/lib64/python3.7/zipfile.py", line 1586, in _open_to_write
self.fp.write(zinfo.FileHeader(zip64))
File "/usr/lib64/python3.7/zipfile.py", line 442, in FileHeader
filename, flag_bits = self._encodeFilenameFlags()
File "/usr/lib64/python3.7/zipfile.py", line 454, in _encodeFilenameFlags
return self.filename.encode('utf-8'), self.flag_bits | 0x800
UnicodeEncodeError: 'utf-8' codec can't encode character '\udcfc' in position 59: surrogates not allowed
Full stacktrace of zipfile:
Traceback (most recent call last):
File "/usr/lib64/python3.7/zipfile.py", line 452, in _encodeFilenameFlags
return self.filename.encode('ascii'), self.flag_bits
UnicodeEncodeError: 'ascii' codec can't encode character '\udcfc' in position 95: ordinal not in range(128)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "run.py", line 41, in <module>
zipdir(path, zipf)
File "run.py", line 16, in zipdir
ziph.write(filepath)
File "/usr/lib64/python3.7/zipfile.py", line 1746, in write
with open(filename, "rb") as src, self.open(zinfo, 'w') as dest:
File "/usr/lib64/python3.7/zipfile.py", line 1473, in open
return self._open_to_write(zinfo, force_zip64=force_zip64)
File "/usr/lib64/python3.7/zipfile.py", line 1586, in _open_to_write
self.fp.write(zinfo.FileHeader(zip64))
File "/usr/lib64/python3.7/zipfile.py", line 442, in FileHeader
filename, flag_bits = self._encodeFilenameFlags()
File "/usr/lib64/python3.7/zipfile.py", line 454, in _encodeFilenameFlags
return self.filename.encode('utf-8'), self.flag_bits | 0x800
UnicodeEncodeError: 'utf-8' codec can't encode character '\udcfc' in position 95: surrogates not allowed
Update:
I've tried some solutions that seemed to work for some at the posted link. This is what I've got:
with
ziph.write(filepath.encode('utf8','surrogateescape').decode('ISO-8859-1')) I got:
Traceback (most recent call last):
File "run.py", line 41, in <module>
zipdir(path, zipf)
File "run.py", line 16, in zipdir
ziph.write(filepath.encode('utf8','surrogateescape').decode('ISO-8859-1'))
File "/usr/lib64/python3.7/zipfile.py", line 1713, in write
zinfo = ZipInfo.from_file(filename, arcname)
File "/usr/lib64/python3.7/zipfile.py", line 506, in from_file
st = os.stat(filename)
FileNotFoundError: [Errno 2] No such file or directory: '/some/path/to/documents/DIS_Broschüre_DE.pdf'
So the encoding/decoding returned something that can not be found in the file system.
The other option: ziph.write(filepath.encode('utf8','surrogateescape').decode('utf-8')) got me
Traceback (most recent call last):
File "run.py", line 41, in <module>
zipdir(path, zipf)
File "run.py", line 16, in zipdir
ziph.write(filepath.encode('utf8','surrogateescape').decode('utf-8'))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 96: invalid start byte
Ok. I've found the Problem.
The files in questen were not the ones I thought they were. Usual umlaus work fine. Somehow the filenames were actually corrupt. like this:
ls in one of the dirs gives:
2e_geh�usetechnologie_flyer_qrcode.pdf
Command line auto completion gives me:
2e_geh$'\344'usetechnologie_flyer_qrcode.pdf
Since these are files that got uploaded via a webinterface I can only imagine that these are made in Windows or another non-UNIX OS and the webserver couldn't handle it.
Other uploaded files had correct umlauts. I'm not shure what happened there but I'm glad it is not Python or the Linux FS to blame.
Thanks for all the tips.

spyder unicode decode error in startup

I was using spyder-ide while parsing a tumblr page with the permission of the author, and at some point everything just crashed. Even my linux system had freezed. Well, to cut to the chase now I can not start spyder, it gives me the following error after I had written spyder to my terminal:
Traceback (most recent call last):
File "/home/dk/anaconda3/bin/spyder", line 2, in <module>
from spyderlib import start_app
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/start_app.py", line 13, in <module>
from spyderlib.config import CONF
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/config.py", line 736, in <module>
subfolder=SUBFOLDER, backup=True, raw_mode=True)
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/userconfig.py", line 215, in __init__
self.load_from_ini()
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/userconfig.py", line 265, in load_from_ini
self.read(self.filename(), encoding='utf-8')
File "/home/dk/anaconda3/lib/python3.5/configparser.py", line 696, in read
self._read(fp, filename)
File "/home/dk/anaconda3/lib/python3.5/configparser.py", line 1012, in _read
for lineno, line in enumerate(fp, start=1):
File "/home/dk/anaconda3/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 0-1: invalid continuation byte
I tried the solution here and I had received the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/spyder.py", line 107, in <module>
from spyderlib.utils.qthelpers import qapplication
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/utils/qthelpers.py", line 24, in <module>
from spyderlib.guiconfig import get_shortcut
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/guiconfig.py", line 22, in <module>
from spyderlib.config import CONF
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/config.py", line 736, in <module>
subfolder=SUBFOLDER, backup=True, raw_mode=True)
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/userconfig.py", line 215, in __init__
self.load_from_ini()
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/userconfig.py", line 265, in load_from_ini
self.read(self.filename(), encoding='utf-8')
File "/home/dk/anaconda3/lib/python3.5/configparser.py", line 696, in read
self._read(fp, filename)
File "/home/dk/anaconda3/lib/python3.5/configparser.py", line 1012, in _read
for lineno, line in enumerate(fp, start=1):
File "/home/dk/anaconda3/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 0-1: invalid continuation byte
I tried uninstalling and reinstalling anaconda and it doesn't seem to work I am open to suggestions, I am very much new to python, so I would appriciate a simple explanation of the possible causes of the error too.
Thanks in advance
Well here is how I solved the issue.
l opened this: spyderlib/userconfig.py
and changed this: self.read(self.filename(), encoding='utf-8')
to this: self.read(self.filename(), encoding='latin-1')
It gave me a Warning: File contains no section headers but started spyder anyway. After that, I closed spyder, opened the terminal and entered spyder --reset then restarted spyder, it seems to work now.
Here is what you should not do at all costs for this problem: thinkering with these, I learned my lesson the hard way:
python3.5/configparser.py
python3.5/codecs.py

Python3: ZipFile instance has no attribute 'extractall'

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.

Resources