Unable to extract some files from zip file - python

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.

Related

PDF file not downloading, JPG file ok

I recently re-activated a python project from last year. It still worked. I recently bought a new computer and upgraded to Windows 11. I also updated my Pycharm to the latest and updated to python 10.1. The program no longer works. Which update is the culprit?
Below shows the essence of the code which does not work. I can successfully download .jpg files but not .pdf files.
from wand.image import Image
file1 = 'U:/Image Files/Bonnie Dundee.jpg'
print(file1)
img1 = Image(filename=file1, resolution=300)
file2 = 'U:/Image Files/Frosty Morning.pdf'
print(file2)
img2 = Image(filename=file2, resolution=300)
The error trace is:
C:\Users\jradc\PycharmProjects\Scratch\venv\Scripts\python.exe C:/Users/jradc/PycharmProjects/Scratch/main.py
U:/Image Files/Bonnie Dundee.jpg
U:/Image Files/Frosty Morning.pdf
Traceback (most recent call last):
File "C:\Users\jradc\PycharmProjects\Scratch\main.py", line 7, in <module>
img2 = Image(filename=file2, resolution=300)
File "C:\Users\jradc\PycharmProjects\Scratch\venv\lib\site-packages\wand\image.py", line 9144, in __init__
self.read(filename=filename)
File "C:\Users\jradc\PycharmProjects\Scratch\venv\lib\site-packages\wand\image.py", line 9815, in read
self.raise_exception()
File "C:\Users\jradc\PycharmProjects\Scratch\venv\lib\site-packages\wand\resource.py", line 222, in raise_exception
raise e
wand.exceptions.DelegateError: FailedToExecuteCommand `"gswin64c.exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r300x300" -dPrinted=false "-sOutputFile=C:/Users/jradc/AppData/Local/Temp/magick-xx4FiylIV-id96p7igp2OZkQb3jLaJ_t%d" "-fC:/Users/jradc/AppData/Local/Temp/magick-wcsbcSOqVJ1dFFk0CAU_gvAVIYrZi1Ut" "-fC:/Users/jradc/AppData/Local/Temp/magick-1GWjBArMZle6Xrv-HTjtyKzCqE1Csf8H"' (The system cannot find the file specified.
) # error/delegate.c/ExternalDelegateCommand/516
Process finished with exit code 1
Could anyone point to what I need to change to get this back to working?
I corrected the file extension.
Looks like the system could not find gswin64c.exe.
py-Wand uses that to convert PDF to PNG.
Check that ghostscript (you probably want the AGPL release) is installed and in your Path.
In the "explorer", search for gswin64c.exe and note the directory where it is.
Then look for "Environment variables" in the start menu and verify that the aforementioned directory is in the Path. If not, edit the path and add it.

How do I download a file from a website in python without the users username?

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

How could I close Excel file using pywinauto

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

libtorrent-python problems, "no such file or directory" when there clearly is

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

Problems with GWTP mergelocales.py

I'm trying to use the mergelocales.py script but I can't get it working.
In my ui.xml (the one that I am using to test it) I have this locales:
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
ui:generateFormat='com.google.gwt.i18n.rebind.format.PropertiesFormat'
ui:generateKeys="com.google.gwt.i18n.rebind.keygen.MD5KeyGenerator"
ui:generateLocales="en_US, es_ES"
ui:defaultLocale="es_ES"
xmlns:g="urn:import:com.google.gwt.user.client.ui">
Then I compile my project using "-extra extras" argument. This generates an "extras" directory in my project wich haves a folder called "myModule" and inside this folder are the generated .properties files:
/myproject/extras/myModule/com.mycompany.myproject.client.mvp.views.MyViewImplMyViewImplUiBinderImplGenMessages_en_US.properties
/myproject/extras/myModule/com.mycompany.myproject.client.mvp.views.MyViewImplMyViewImplUiBinderImplGenMessages_es_ES.properties
Now, I am trying to run the mergelocales.py to get the LocalizableResources*.properties files and when I try to run:
python2.5 ~/Downloads/mergelocales.py extras/myModule src/main/java/com/google/gwt/i18n/client/
I get the following stack trace:
Skipping non-default locale in extra directory: com.mycompany.myproject.client.mvp.views.MyViewImplMyViewImplUiBinderImplGenMessages_en_US.properties
Skipping non-default locale in extra directory: com.mycompany.myproject.client.mvp.views.MyViewImplMyViewImplUiBinderImplGenMessages_es_ES.properties
Traceback (most recent call last):
File "/Users/admin/Downloads/mergelocales.py", line 497, in <module>
main()
File "/Users/admin/Downloads/mergelocales.py", line 131, in main
mergeLocales( args[0], args[1] )
File "/Users/admin/Downloads/mergelocales.py", line 466, in mergeLocales
pathname = os.path.join( resourcesDir, defaultLocaleFilename )
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/posixpath.py", line 60, in join
if b.startswith('/'):
AttributeError: 'NoneType' object has no attribute 'startswith'
Is there something that I am missing to get this script working?
Now, I am trying to run the mergelocales.py to get the LocalizableResources*.properties files and [...] I get the following stack trace...
You have to create the blank LocalizableResource.properties files manually yourself and they will be populated by mergeLocales script. See here:
Let’s say we would like our application to also be available in French. We need to create two empty files named LocalizableResource.properties and LocalizableResource_fr.properties. The LocalizableResource.properties will contain the default (English) texts, whereas the LocalizableResource_fr.properties, will contain the French texts. Those files should be located in src/main/resources/com/google/gwt/i18n/client.
from http://blog.arcbees.com/2015/09/15/i18n-using-gwts-uibinder/

Categories