Hi so im building a script which downloads a .exe file via urllib. Sadly,the downloaded file, when executed, shows an error prompt by windows, the file would be damaged or corrupted and cannot be executed.
My script:
url = 'https://anonfiles.com/51l035B5x5/npp.8.1.9.3.Installer.x64_exe'
f = urllib.request.urlopen(url)
file = f.read()
f.close()
f2 = open('npp.exe', 'wb')
f2.write(file)
f2.close()
how to proceed so the file is working when downloaded?
Try downloading it like this instead:
import urllib.request
urllib.request.urlretrieve("https://anonfiles.com/51l035B5x5/npp.8.1.9.3.Installer.x64_exe", "npp.exe")
If that doesn't work, the executable might be broken itself.
Related
I am getting a permission's error when writing multiple files to a specific directory from a url, specifically an SFTP site. Here is an example of what I am trying to run.
import pycurl, urllib2, requests, json, pprint, urllib, os
from io import BytesIO
files = []
c = pycurl.Curl()
data = BytesIO()
c.setopt(c.URL, "https://sftp.mmm.com:443/api/v1.1/files/Source_Directory/")
c.setopt(c.USERPWD, "username:password")
c.setopt(c.WRITEDATA, data)
c.perform()
c.close()
dictionary = json.loads(data.getvalue())
for i in dictionary['files']:
files.append(i["fileName"])
for x in files:
c = pycurl.Curl()
file = open(x, "a+")
c.setopt(c.URL, "https://sftp.pjm.com:443/api/v1.1/files/Source_directory/"+x)
c.setopt(c.USERPWD, "username:password")
c.setopt(c.WRITEDATA, file)
c.perform()
c.close()
file.close()
When i use "x" in the file - open (x, "a+") the files write to my python project folder. However if I replace the x with a specific directory like my desktop I get IOError: [Errno 13] Permission denied: "Destination_Directory"
This is most likely because what you are doing requires administrator. Simply run your IDE as administrator (by right clicking and clicking run as administrator). If you are running it from command prompt, run command prompt as administrator.
Trying to save a xml file to a into a zip file, but I'm getting an error with the directory. I have the following code:
if not os.path.exists(log_file_path):
os.makedirs(log_file_path)
for s in xml_list:
parent_file = zipfile.ZipFile(zip_file_name, "w")
urllib.urlretrieve(log_repository_url + "/r.xml", zip_file_name + "\\r.xml")
parent_file.close()
The error is saying I don't have r.xml in the zip file. Shouldn't this code create the .xml file and write to it? If not, how should I proceed?
Thank you!
Question: The error is saying I don't have r.xml in the zip file.
You have to write it to your ZipFile, for instance:
with ZipFile(zip_file_name, 'w') as myzip:
local_filename, headers =
urllib.request.urlretrieve(log_repository_url + "/r.xml")
myzip.write(local_filename, arcname="r.xml")
Python ยป 3.6.1 Documentation:
urllib.request.urlretrieve
zipfile.ZipFile.write
How to download a torrent file and save it in a user defined directory using urllib 2 ?
Instead of doing the work yourself using urllib2.urlopen, I recommend using urllib.urlretrieve as that will do the work behind the scenes.
That is not so important that you want download torrent file.
You can download and save file of any type this way:
import urllib2
with open("mytorrent", "w") as f:
f.write(urllib2.urlopen('http://megatorrent.com/torrent-url').read())
File from http://megatorrent.com/torrent-url will be saved in the current directory as mytorrent.
When you want to save the file in other directory you do something like:
import urllib2
with open(os.path.join(torrents_die_path, "mytorrent"), "w") as f :
f.write(urllib2.urlopen('http://megatorrent.com/torrent-url').read())
I'm working on a script that will automatically update an installed version of Calibre. Currently I have it downloading the latest portable version. I seem to be having trouble saving the zipfile. Currently my code is:
import urllib2
import re
import zipfile
#tell the user what is happening
print("Calibre is Updating")
#download the page
url = urllib2.urlopen ( "http://sourceforge.net/projects/calibre/files" ).read()
#determin current version
result = re.search('title="/[0-9.]*/([a-zA-Z\-]*-[0-9\.]*)', url).groups()[0][:-1]
#download file
download = "http://status.calibre-ebook.com/dist/portable/" + result
urllib2.urlopen( download )
#save
output = open('install.zip', 'w')
output.write(zipfile.ZipFile("install.zip", ""))
output.close()
You don't need to use zipfile.ZipFile for this (and the way you're using it, as well as urllib2.urlopen, has problems as well). Instead, you need to save the urlopen result in a variable, then read it and write that output to a .zip file. Try this code:
#download file
download = "http://status.calibre-ebook.com/dist/portable/" + result
request = urllib2.urlopen( download )
#save
output = open("install.zip", "w")
output.write(request.read())
output.close()
There also can be a one-liner:
open('install.zip', 'wb').write(urllib.urlopen('http://status.calibre-ebook.com/dist/portable/' + result).read())
which doesn't have a good memory-efficiency, but still works.
If you just want to download a file from the net, you can use urllib.urlretrieve:
Copy a network object denoted by a URL to a local file ...
Example using requests instead of urllib2:
import requests, re, urllib
print("Calibre is updating...")
content = requests.get("http://sourceforge.net/projects/calibre/files").content
# determine current version
v = re.search('title="/[0-9.]*/([a-zA-Z\-]*-[0-9\.]*)', content).groups()[0][:-1]
download_url = "http://status.calibre-ebook.com/dist/portable/{0}".format(v)
print("Downloading {0}".format(download_url))
urllib.urlretrieve(download_url, 'install.zip')
# file should be downloaded at this point
have you tryed
output = open('install.zip', 'wb') // note the "b" flag which means "binary file"
I try to use urllib to crawl this file: http://www.anzhi.com/dl_app.php?s=68611, but always download a wrong file(size smaller). However, if I open up this link on chrome, it goes well and the downloaded file size is correct. The code is attached, what's the problem?
import urllib
apk = "http://sc.hiapk.com/Download.aspx?aid=294091"
local=r'x.apk'
webFile = urllib.urlopen(apk)
localFile = open(local, "w")
realurl = webFile.geturl()
print realurl
realFile = urllib.urlopen(realurl)
localFile.write(realFile.read())
webFile.close()
realFile.close()
localFile.close()
What OS are you on? This line of code:
localFile = open(local, "w")
opens a text-mode file on Windows, which will do things that you don't want. Does changing that to
localFile = open(local, "wb")
(opening the file in binary mode) make things work correctly?
You're not using the same URL in your code that you're asking about in the question. Use the anzhi.com URL and you'll get the file you want. :)