I want to extract the tif file from a range of URL. This code works for one zip file, but if I want to extract zips in a range(1,43) it doesn't work
the error is:
BadZipFile: File is not a zip file
Could somebody help me?
print('Downloading started')
for number in range(1,3):
url = f'https://downloadagiv.blob.core.windows.net/dhm-vlaanderen-ii-dsm-raster-1m/DHMVIIDSMRAS1m_k{number}.zip'
req = requests.get(url)
# Split URL to get the file name
filename = url.split('/')[-1]
req = requests.get(url)
print('Downloading Completed')
zipfile= ZipFile(BytesIO(req.content))
listOfFileNames = zipfile.namelist()
for filename in listOfFileNames:
# Check filename endswith tif
if filename.endswith('.tif'):
# Extract a single file from zip
zipfile.extract(filename, '/content/gdrive/My Drive')
Related
I need to download and view file (if possible, for Image, PDF, etc) using Flutter.
My problem is, the file that I want to download is Base64 String.
How can I achieve that using Flutter??
Following is the code snippet to decode base64 string and save it as a file on the local device. please note in terms of viewing files, image and pdf would require different libraries.
Future<String> _createFileFromString() async {
final encodedStr = "put base64 encoded string here";
Uint8List bytes = base64.decode(encodedStr);
String dir = (await getApplicationDocumentsDirectory()).path;
File file = File(
"$dir/" + DateTime.now().millisecondsSinceEpoch.toString() + ".pdf");
await file.writeAsBytes(bytes);
return file.path;
}
Google Chrome does not let open tab with Url base64 encode, like "data:application/pdf;base64,JVBERi0xLjQKJeLj..." with javascript, so you can not open url with url_launcher, but you can download file.
Here is my code:
import 'dart:html' as html;
...
Future<void> downloadFile() {
final urlString = "data:application/pdf;base64,JVBERi0xLjQKJeLj...";
html.AnchorElement anchorElement = html.AnchorElement(href:urlString);
anchorElement.download = urlString;
anchorElement.click();
}
So, I'm downloading some data files from an ftp server. I need to daily go in and retrieve new files and save them on my pc, but only the new ones.
Code so far:
from ftplib import FTP
import os
ftp = FTP('ftp.example.com')
ftp.login()
ftp.retrlines('LIST')
filenames = ftp.nlst()
for filename in filenames:
if filename not in ['..', '.']:
local_filename = os.path.join('C:\\Financial Data\\', filename)
file = open(local_filename, mode = 'x')
ftp.retrbinary('RETR '+ filename, file.write)
I was thinking of using if not os.path.exists() but I need the os.path.joint for this to work. Using open() with mode = 'x' as above, I get the following err message: "FileExistsError: [Errno 17] File exists"
Is error handling the way to go, or is there a neat trick that I'm missing?
I landed on the following solution:
filenames_ftp = ftp.nlst()
filenames_loc = os.listdir("C:\\Financial Data\\")
filenames = list(set(filenames_ftp) - set(filenames_loc))
I'm uploading a file that is a zip in a web app and passing it as type "Part" and I need to grab the name of the file that I originally uploaded. I can't seem to figure out for the life of me how to grab the actual name of the uploaded file. I've tried the following assuming my Part is uploaded with the original file name as "ABCD". My Part object will be named "file":
file.getHeaderNames() yields "content-type" and "content-disposition"
file.getName() yields "BPzip8237267963573706108tmp" which is the temp file's name
Any ideas on how I would go about doing this?
// define variable for file name
String filename = "";
// get part
Part file = request.getPart("file");
// get filename from part header
for (String s: file.getHeader("content-disposition").split(";")) {
if (s.trim().startsWith("filename")) {
filename = s.split("=")[1].replace("\"", "");
break;
}
}
i have blueprint like below, and using flask-upload for uploading file
#blueprint.route('/', methods=['GET', 'POST'])
def upload_file1():
# user = User.query.filter_by(id=current_user.id).first_or_404()
form = PhotoFormUpload()
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
foto = form.photo_upload.data.lower()
filename = user_photos.save(foto)
update_avatar = User.query.filter_by(id=current_user.id).update(dict(avatar=filename))
db.session.commit()
flash('Upload Success', category='success')
return render_template('upload/display_photo.html', filename=filename)
else:
return render_template('upload/upload.html', form=form)
i change
foto = form.photo_upload.data
to
foto = form.photo_upload.data.lower()
but it doesnt works
how do i rename uploaded file name?
Answer on your question exist in http://pythonhosted.org/Flask-Uploads/
save(storage, folder=None, name=None)
Parameters:
storage – The uploaded file to save.
folder – The subfolder within the upload set to save to.
name – The name to save the file as. If it ends with a dot, the file’s extension will be appended to the end.
Example: user_photos.save(pathToDirectory, name=NewName)
I used the following method to rename the uploaded file on the fly
file = request.files['file']
file.filename = "abc.txt" #some custom file name that you want
file.save("Uploads/"+file.filename)
I have properly implemented InboundMailHandler and I'm able to process all other mail_message fields except mail_message.attachments. The attachment filename is read properly but the contents are not being saved in the proper mime_type
if not hasattr(mail_message, 'attachments'):
raise ProcessingFailedError('Email had no attached documents')
else:
logging.info("Email has %i attachment(s) " % len(mail_message.attachments))
for attach in mail_message.attachments:
filename = attach[0]
contents = attach[1]
# Create the file
file_name = files.blobstore.create(mime_type = "application/pdf")
# Open the file and write to it
with files.open(file_name, 'a') as f:
f.write(contents)
# Finalize the file. Do this before attempting to read it.
files.finalize(file_name)
# Get the file's blob key
blob_key = files.blobstore.get_blob_key(file_name)
return blob_key
blob_info = blobstore.BlobInfo.get(blob_key)
`
When I try to display the imported pdf file by going to the url: '/serve/%s' % blob_info.key()
I get a page with what seems like encoded data, instead of the actual pdf file.
Looks like this:
From nobody Thu Aug 4 23:45:06 2011 content-transfer-encoding: base64 JVBERi0xLjMKJcTl8uXrp/Og0MTGCjQgMCBvYmoKPDwgL0xlbmd0aCA1IDAgUiAvRmlsdGVyIC9G bGF0ZURlY29kZSA+PgpzdHJlYW0KeAGtXVuXHLdxfu9fgSef2RxxOX2by6NMbSLalOyQK+ucyHpQ eDE3IkWKF0vJj81vyVf3Qu9Mdy+Z40TswqKAalThqwJQjfm1/Hv5tWzxv13blf2xK++el+/LL+X+ g/dtefq
Any ideas? Thanks
Email's attachments are EncodedPayload objects; to get the data you should call the decode() method.
Try with:
# Open the file and write to it
with files.open(file_name, 'a') as f:
f.write(contents.decode())
If you want attachments larger 1MB to be processed successfully, decode and convert to str:
#decode and convert to string
datastr = str(contents.decode())
with files.open(file_name, 'a') as f:
f.write(datastr[0:65536])
datastr=datastr[65536:]
while len(datastr) > 0:
f.write(datastr[0:65536])
datastr=datastr[65536:]
Found the answer in this excellent blob post:
http://john-smith.appspot.com/app-engine--what-the-docs-dont-tell-you-about-processing-inbound-mail
This is how to decode an email attachment for GAE inbound mail:
for attach in mail_message.attachments:
filename, encoded_data = attach
data = encoded_data.payload
if encoded_data.encoding:
data = data.decode(encoded_data.encoding)