Saving urlretrieve in to zip file python - python

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

Related

Python Download - file corrupted?

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.

Using Shareplum to download, alter and then upload a file to sharepoint

Exactly as the title says, I have this code
from shareplum import Site
from shareplum import Office365
from shareplum.site import Version
authcookie = Office365('https://mysite.sharepoint.com/', username='username', password='password').GetCookies()
site = Site('https://mysite.sharepoint.com/sites/mysite/', version=Version.v2016, authcookie=authcookie)
folder = site.Folder('Shared Documents/Beta Testing')
file = folder.get_file('practice.xlsx')
with open("practice.xlsx", "wb") as fh:
fh.write(file)
print('---')
folder.upload_file('xlsx', 'practice.xlsx')
Currently it downloads the file just fine which is fantastic, however I do not know how to reverse what I did with opening and downloading the file. Basically I need to be able to upload the file with the exact same name as the one I downloaded in the exact same format (in this case xlsx) as to overwrite the one in the sharepoint with the updated document.
Your post indicates that you want to modify the file so you will need some file handling for the downloaded file once it is saved after modification. Once the file modification has been done you need to open the file in 'rb' and then read that to a variable which will be the content when calling folder_obj.upload_file(content, name).
#this is your step to modify the file.
with open("practice.xlsx", "wb") as fh:
#file modification stuff... pyxlsx?
fh.write(file)
#open the file and read it into a variable as binary
with open("practice.xlsx", "rb") as file_obj:
file_as_string = file_obj.read()
#upload the file including the file name and the variable (file_as_string)
folder.upload_file(file_as_string, 'practice.xlsx')
This has been working for me. If you want to change the name of the file to include a version, delete the old file by calling folder.delete_file("practice.xlsx").
Can you try the below and see if it works?
with open("practice.xlsx", "wb") as fh:
file_content = fh.write(file)
folder.upload_file(file_content,'practice.xlsx')

How to download multiple zip files using requests and extracting to a folder using Python

I have a url_file.txt file with few links to download the zip files directly. The content of url_file.txt file has 6 urls.So I have to take each url from this file and get the zip file. Then extract it to the destination folder. The destination folder contains all the extracted application extensions. So the code I have written is:
import os, io, zipfile, requests
cwd = os.getcwd()
save_path = cwd +"\\test"
os.chmod(save_path,0o777)
with open('url_file.txt', 'r') as urll:
for i,val in enumerate(urll):
path = val
r = requests.get(path)
z = zipfile.ZipFile(io.BytesIO(r.content))
z.extractall(save_path)
But I am getting error while executing this. If I pass a url directly in requests. It is downloading and extracting the application. I dont know what I am doing wrong. Someone please give me some suggestion to resolve this. The error I am getting while executing the above code is:
z = zipfile.ZipFile(io.BytesIO(r.content))
File "C:\Python\Python39\lib\zipfile.py", line 1257, in __init__
self._RealGetContents()
File "C:\Python\Python39\lib\zipfile.py", line 1324, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
I have converted txt files content to array and passed to the requests. Added these two lines and worked perfectly.
text_file = open('url_file.txt', 'r')
lines = text_file.read().split(',')

File or directory not found even though it exists

this is my code to open a file and get the text from it :
f = open("C:/Users/muthaharsh/Documents/Harsh/News
Project/Part3/Testing_purposes/Downloads3/Are-you-being-churned-,-
Mint.txt","r+")
text = f.readlines()
print(text)
but i keep getting the error :
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/muthaharsh/Documents/Harsh/News Project/Part3/Testing_purposes/Downloads3/Are-you-being-churned-,-Mint.txt'
What code do i write to be able to do this ?
Thanks in advance ...
It's the whitespace in the path to file. Either use
r"C:/Users/muthaharsh/Documents/Harsh/News Project/Part3/Testing_purposes/Downloads3/Are-you-being-churned-,-Mint.txt"
or remove the whitespace in the filepath.
If you are running the code on windows add r before your filepath string.
Another way is that you can provide your input file as system arguments.
import sys
file_name = sys.argv[1]
with open(file_name, 'r') as f1:
file_text = f1.read()
print(file_text)
eg: python program for reading the file
you can run the code considering script is saved as readFile.py:
D:\Programs>python readFile.py D:\test.txt
output: This is a sample file
Here is the text

Python: Use Dropbox API - Save .ODT File

I'm using Dropbox API with Python. I don't have problems with Dropbox API, I make all the authentification steps without problems.
When I use this code:
pdf_dropbox = client.get_file('/Example.pdf')
new_file = open('/home/test.pdf','w')
new_file.write(pdf_dropbox.read())
I generate a file in the path /home/test.pdf, it's a PDF file and the content is displayed same as original.
But when I try same code with an .odt file, it fails generating the new file:
odt_dropbox = client.get_file('/Example.odt')
new_file = open('/home/test_odt.odt','w')
new_file.write(odt_dropbox.read())
This new file test_odt.odt has errors and I can't see it's content.
# With this instruction I have the content of the odt file inside odt_dropbox
odt_dropbox = client.get_file('/Example.odt')
Wich is the best way to save the content of an odt file ?
Is there a better way to write LibreOffice files ?
I'd appreciate any helpfull information,
Thanks
Solved, I forgot 2 things:
Open the file for binary writing wb instead of w
new_file = open('/home/test_odt.odt','wb')
Close the file after creation: new_file.close() to make the flush
Full Code:
odt_dropbox = client.get_file('/Example.odt')
new_file = open('/home/test_odt.odt','wb')
new_file.write(odt_dropbox.read())
new_file.close()

Categories