I have made a python script which takes the latest files from university e-class (lectures in pdf formats, scripts etc) via requests and downloads them. After downloading it automatically extracts the zip on the specific folder i want. But the extraction function sometimes it gets stuck on the same files.
The function is this:
import zipfile
from tqdm import tqdm
zipnm = 'Διαδικτυακά και Φορητά Πληροφοριακά Συστήματα'
quartls = '6'
def extractZip(zipName, quartInd):
with zipfile.ZipFile('./'+zipName+'.zip', 'r') as zip_ref:
for member in tqdm(zip_ref.infolist(), desc='Extracting '):
try:
zip_ref.extract(member, './'+quartInd+'° εξάμηνο/'+zipName)
except zipfile.error as e:
print(e)
if __name__ == '__main__':
extractZip(zipnm, quartls)
When running on terminal it throws this:
PS Microsoft.PowerShell.Core\FileSystem::\\192.168.1.200\[REDACTED]> python .\test.py
Extracting : 39%|███████████████████████████ | 11/28 [00:00<00:01, 12.04it/s]
Traceback (most recent call last):
File "\\192.168.1.200\[REDACTED]\test.py", line 18, in <module>
extractZip(zipsd, quartsd)
File "\\192.168.1.200\[REDACTED]\test.py", line 12, in extractZip
zip_ref.extract(member, './'+quartInd+'° εξάμηνο/'+zipName)
File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python39\lib\zipfile.py", line 1616, in extract
return self._extract_member(member, path, pwd)
File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python39\lib\zipfile.py", line 1683, in _extract_member
os.mkdir(targetpath)
FileExistsError: [WinError 183] Δεν είναι δυνατή η δημιουργία ενός αρχείου όταν αυτό το αρχείο υπάρχει ήδη: '6° εξάμηνο\\Διαδικτυακά και Φορητά Πληροφοριακά Συστήματα\\Εργαστήρια\\Lab 5 - Introduction to PHP'
When I'm trying to extract the zip file manually, it sticks on 2 files that I either can retry or abort the files from extracting. Although the other files that already exist show me the options to replace them. My question is, why is this happening on these 2 files and not letting me to replace them like the others? Are the files corrupted (although I checked their size and looking if EOF is missing but nothing suspicious)?
P.S. I don't want on my script file to check if exists to exclude them I just want to find the source of the problem so I can act accordingly.
P.S.#2:
The files that get extracted are actually in an Ubuntu server machine which I access via Samba on Windows and then I run the script or doing anything with the files.
So I am trying to make a download tool to download stuff via a link here is a snippet from my code
if download_choice == "14":
print("Downloading test file to Desktop...")
myfile14 = requests.get(url14)
open('c:/users/%userprofile%/desktop/', 'wb').write(myfile14.content)
I want to make a program that can download stuff via a link. I want it to run on all PCs not only on mine. Here is the error:
Traceback (most recent call last): File "C:\Users\David\Desktop\Windows Download Tool\Python\WDT.py", line 100, in <module> open('c:/users/%userprofile%/desktop/', 'wb').write(myfile14.content) FileNotFoundError: [Errno 2] No such file or directory: 'c:/users/%userprofile%/desktop/'
Python does not use %userprofile% to get the username of the executing user.
To achieve this, you need to import the package os and run it's getlogin function. This returns a string with the current username
Example:
import os
username = os.getlogin()
if download_choice == "14":
print("Downloading test file to Desktop...")
myfile14 = requests.get(url14)
open(f'C:/Users/{username}/desktop/', 'wb').write(myfile14.content)
I am using an f-string for opening the file, since it is preferred by PEP-8 (it's just correct code-styling)
Attention: This only works on a windows machine
I have a program, in which I need to convert a PDF to an image using Image Magick. I do that using the subprocess package:
cmd = 'magick convert -density 300 '+pdfFile+'['+str(rangeTuple[0])+'-'+str(rangeTuple[1])+'] -depth 8 '+'temp.tiff' #WINDOWS
if(os.path.isfile('temp.tiff')):
os.remove('temp.tiff')
subprocess.call(cmd,shell=True)
im = Image.open('temp.tiff')
The error I got is:
convert-im6.q16: not authorized `temp2.pdf' # error/constitute.c/ReadImage/412.
convert-im6.q16: no images defined `temp.tiff' # error/convert.c/ConvertImageCommand/3258.
Traceback (most recent call last):
File "UKExtraction2.py", line 855, in <module>
doItAllUpper("A0","UK5.csv","temp",59,70,"box",2,1000,firstPageCoordsUK,boxCoordUK,voterBoxCoordUK,internalBoxNumberCoordUK,externalBoxNumberCoordUK,addListInfoUK)
File "UKExtraction2.py", line 776, in doItAllUpper
doItAll(tempPDFName,outputCSV,2,pdfs,formatType,n_blocks,writeBlockSize,firstPageCoords,boxCoord,voterBoxCoord,internalBoxNumberCoord,externalBoxNumberCoord,addListInfo,pdfName)
File "UKExtraction2.py", line 617, in doItAll
mainProcess(pdfName,(0,noOfPages-1),formatType,n_blocks,outputCSV,writeBlockSize,firstPageCoords,boxCoord,voterBoxCoord,internalBoxNumberCoord,externalBoxNumberCoord,addListInfo,bigPDFName,basePages)
File "UKExtraction2.py", line 542, in mainProcess
im = Image.open('temp.tiff')
File "/home/rohit/.local/lib/python3.6/site-packages/PIL/Image.py", line 2609, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'temp.tiff'
The most important of which is:
convert-im6.q16: not authorized `temp2.pdf' # error/constitute.c/ReadImage/412.
I think this is because ImageMagick isn't authorized to access the PDF. What should be done now? I'm on a Linux server. Any help is appreciated.
emcconville is correct. More specifically edit the Imagemagick policy.xml file to uncomment this line:
<!-- <policy domain="module" rights="none" pattern="{PS,PDF,XPS}" /> -->
And change it from rights="none" to rights="read|write"
<policy domain="module" rights="read|write" pattern="{PS,PDF,XPS}" />
This was a recent addition to the policy.xml file, I believe, due to a security flaw found in the Ghostscript delegate. I think that flaw has now been fixed in the current version of Ghostscript, which is 9.25.
NOTE: On some systems the policy line will have domain="coder" rather than domain="module"
Quick and easy solution:
sudo mv /etc/ImageMagick-6/policy.xml /etc/ImageMagick-6/policy.xml.off
When done, you can restore the original with
sudo mv /etc/ImageMagick-6/policy.xml.off /etc/ImageMagick-6/policy.xml
I am using Dockerfile to update an image, and suddenly I got the policy.xml file in my way. although the version of Ubuntu (xenial) was the same and ImageMagick as well.
I ended up removing the single line causing my problem.
RUN sed -i 's/^.*policy.*coder.*none.*PDF.*//' /etc/ImageMagick-6/policy.xml
hope this helps someone
Use the below command to delete the policy file to fix it, If required you can also take backup of this policy file.
rm /etc/<ImageMagick_PATH>/policy.xml
for me it was ImageMagick6 and the command was :
sudo rm /etc/ImageMagick-6/policy.xml
I'm having a problem that I can't excel file.
I was using swapy+pywinauto.
program export excel file with different name (ex. time..)
I used swapy to close the export excel.
from pywinauto.application import Application
app = Application().Start(cmd_line=u'"C:\\Program Files\\Microsoft Office\\Office14\\EXCEL.EXE" \\dde')
xlmain = app.XLMAIN
xlmain.Wait('ready')
xlmain.Close()
app.Kill_()
but got error below.
Traceback (most recent call last):
File "D:/23007.py", line 54, in <module>
xlmain.Wait('ready')
WaitUntil(timeout, retry_interval, lambda: self.__check_all_conditions(check_method_names))
File "C:\Python35\lib\site-packages\pywinauto\timings.py", line 308, in WaitUntil
raise err
pywinauto.timings.TimeoutError: timed out
Process finished with exit code 1
Why do you use app.XLMAIN? Is the title of window similar to XLMAIN? Usually the title is <file name> - Excel so that pywinauto can handle it so: xlmain = app["<file name> - Excel"].
Obviously Wait('ready') raised an exception because the window with title "XLMAIN" or similar is not found.
Generally I would recommend using pyWin32 standard module win32com.client to work with Excel (through standard COM interface). See the second answer here for example: Driving Excel from Python in Windows
First of all, here's the code
#!/usr/bin/env python3.4
import libtorrent as lt
import os
fs = lt.file_storage()
lt.add_files(fs, "/var/mirror/packages/") # There are two files in this directory
t = lt.create_torrent(fs, flags = 1&8&16) # 1 = Optimization, 8 = Symbolic links, 16 = calculate file hashes.
t.add_tracker("udp://tracker.[private].com:80")
print(os.path.isdir("/var/mirror/packages/"))
lt.set_piece_hashes(t,"/var/mirror/packages/")
print(t.generate())
And here's what happens when I run it
True
Traceback (most recent call last):
File "./test.py", line 9, in <module>
lt.set_piece_hashes(t,"/var/mirror/packages/")
RuntimeError: No such file or directory
This is the page I got this from
I have browsed around the bindings, but I can't find the set_piece_hashes sources. It returns the same error code when I change the path to "." or "/" (keeping the add_files path the same)
Anyone know what I'm doing wrong? I can't find any sort of documentation other than the site I linked above
Turns out set_piece_hashes wants the parent directory of the directory you created the filestore with. After I fixed that, I now get another error, which is a known bug in libtorrent here