How can I create a zip? - boto

How can I add files and zip the folder?
So far, using zipfile, I’m able to create a zip.

How can I create a folder to add files and zip the folder?
You can do what you want by following these steps:
To create a folder, use os.mkdir like this:
import os
os.mkdir('spam') # your folder name
then you can write files to it or use shutil.copy to make copies of existent files.
# to write files:
with open('./spam/eggs.txt', 'w') as fp: # creates a text file in spam named eggs.txt
fp.write('hello') # write hello
# to copy files
import shutil
shutil.copy('eggs.txt', './spam/eggs.txt') # copy eggs.txt from the current working dir to spam folder
then to zip the folder, you had the right idea, to use zipfile like this:
from zipfile import ZipFile, ZIP_DEFLATED
zipfp = ZipFile('spam.zip', 'w', ZIP_DEFLATED) # create a new zip file named spam.zip
for root, dirname, files in os.walk('./spam'): # walk in the spam folder
[zipfp.write(os.path.join('./spam',file)) for file in files] # write them to the zip file
zipfp.close()
A complete example:
import os
from zipfile import ZipFile, ZIP_DEFLATED
os.mkdir('spam')
with open('./spam/eggs.txt', 'w') as fp:
fp.write('hello')
with ZipFile('spam.zip', 'w', ZIP_DEFLATED) as zipfp:
for root, dirname, files in os.walk('./spam'):
[zipfp.write(os.path.join('./spam',file)) for file in files]
The result of this code is it produces a compressed folder named spam.zip and in the zip file, there's a txt file named eggs.txt which contains the text 'hello'.
To clarify for the OP:
[zipfp.write(os.path.join('./spam',file)) for file in files]
is equivalent to:
for file in files:
zipfp.write(os.path.join('./spam',file))
it's just simpler to write it using list comprehension.

Related

How can I save a file into a folder in Jupyter Notebook?

I'm working on a ipynb file, here I created a dataframe and converted it into a csv file. I want create this file in a particular existing folder.
For example:
df = pd.DataFrame(#Something)
df.to_csv("data.csv")
But I want create this csv file in a folder contained in the jupyter notebook i'm working on

How to allow user to download entire folder, directory, or at least multiple images? (Flask)

I am trying to figure out how to allow user to download a folder that contains multiple images when the user presses download button. For now, I can only use send_file code to allow user to download a single image. Is there any possible way to do this?
#app.route('/download')
def download():
path = "where my image is"
return send_file(path, as_attachment=True)
This is my current code and I want to change it.
I would advise you to archive the directory and then send it.
A list of all files in the folder is created.
These files are then compressed in a zip archive that is written to a stream.
The stream is then transmitted via send_file.
from flask import send_file
from zipfile import ZipFile
from io import BytesIO
import os, glob
#app.route('/download', methods=['GET'])
def download():
path = # target directory
root = os.path.dirname(path)
files = glob.glob(os.path.join(path, '*'))
stream = BytesIO()
with ZipFile(stream, 'w') as zf:
for f in files:
zf.write(f, os.path.relpath(f, root))
stream.seek(0)
return send_file(
stream,
as_attachment=True,
attachment_filename='archive.zip',
mimetype='application/zip'
)

Python Os.walk misses few files to process in the directory

Out of 10 files in the directory, only 8 files are processed and 2 files are not processed. But if I delete all the 8 files and try running with the missed 2 files it is working. Why Os.walk is missing files? Also is there a way to process all the files in the directory one after another without missing any.
Note: The solution will be used for the folder that contains 100K JSON files.
for root, dirs, files in os.walk('D:/M'):
for file in files:
if file.endswith(".json"):
Strfil=os.path.join(root,file)
with open(Strfil, 'r') as json_file:
For file system related things it is better to use the pathlib module
With pathlib you can do something like this.
from pathlib import Path
json_files = list(Path("D:/M").glob("**/*.json"))
for f in json_files:
with open(f, 'r') as json_file:
I think any file with more than 250 characters will be skipped by Windows as 'too long'. What I suggest is to map the network drive to make the path much shorter.
e.g. z:\myfile.xlsx instead of c:\a\b\c\d\e\f\g\myfile.xlsx

Colab how to get file id for existing file

I am starting with colab for ml and I have problem importing files from my google drive into the notebook. Say I got a file pretrained_vgg19.mat in my drive like drive/jupyter/pretrained_vgg19.mat. The code snippet for importing files from drive says that I need to use the file_ID that looks like laggVyWshwcyP6kEI-y_W3P8D26sz. How do I get this file_ID?
See PyDrive documentation for the ListFile command:
from pydrive.drive import GoogleDrive
drive = GoogleDrive(gauth) # Create GoogleDrive instance with authenticated GoogleAuth instance
# Auto-iterate through all files in the root folder.
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))
Now all you need to do is tweak the search parameters, since you know the title of the file already. See docs.
file_list = drive.ListFile({'q': "name='pretrained_vgg19.mat' and trashed=false"}).GetList()
for file in file_list:
print('%s' % (file['id']))
Note that it is possible to have files with the same folder name and file name, because you can create multiple folders with identical paths in Google Drive. If there is even a chance of this, you will get multiple files returned in your list operation and will need to use some other criteria in order to select the correct one.
user244343's answer didn't work for me since the gauth object doesn't exist. I did this instead (test.zip needs to point to the right folder and file in your Drive!):
!apt-get install -qq xattr
filename = "/content/drive/My\ Drive/test.zip"
# Retrieving the file ID for a file in `"/content/drive/My Drive/"`:
id = !xattr -p 'user.drive.id' {filename}
print(id)

Pandas module read_csv reads file within Eclipse+pydev while fail if I run standalone

I'm currently developing a GUI using Python and Tkinter.
On of the task is to open and read some *.csv files.
I order to perform this task I have written the following code:
ReadData=pd.read_csv(ResultFile,skipinitialspace=True).values
While I'm running the code within the IDE Eclipse+Pydev everything work fine. But as soon as I run my code form a Dos window, i.e. python MainGrap.py, the code bugs stating that the file doesn't exists???????
I first load the path to a file via self.Inp_Filename=askopenfilename() then I create a list of the folders by means of the following function:
def PathDisintegrator(Inp_File):
Folders = os.path.split(Inp_File)
LastFolder = Folders[1]
RootPath = Folders[0]
Dirs=[]
while not(LastFolder==''):
Dirs.insert(0,LastFolder)
Folders = os.path.split(RootPath)
LastFolder = Folders[1]
RootPath = Folders[0]
Dirs.insert(0,RootPath[:-1])
Dirs=Dirs[:-1]
return(Dirs)
Then I can recreate the full path to file via the following function:
def PathAndFile(Folders,File):
FileOut=''
for item in Folders:
FileOut=FileOut+os.sep+item
#FileOut=FileOut+r"\\"+item
FileOut=FileOut[1:]+os.sep+File
return(FileOut)
I have printed out the file path even within the parser of Pandas and it looks fine to me: D:\Abaqus_Runs\DOWLEX_PET_LAMINATE_PROTO_REFERENCE_SI_Version_2_Revision_2_MDangle0_Rate0_01_MOVING_NODE_out.csv
The problem here is that your python environment in eclipse can see the folder where your csv resides but the terminal one does not.
You can observe what the system paths are by doing:
In [331]:
import sys
sys.path
Out[331]:
['',
'C:\\WinPython-64bit-3.4.2.4\\python-3.4.2.amd64\\python34.zip',
'C:\\WinPython-64bit-3.4.2.4\\python-3.4.2.amd64\\DLLs',
'C:\\WinPython-64bit-3.4.2.4\\python-3.4.2.amd64\\lib',
'C:\\WinPython-64bit-3.4.2.4\\python-3.4.2.amd64',
'C:\\WinPython-64bit-3.4.2.4\\python-3.4.2.amd64\\lib\\site-packages',
'C:\\WinPython-64bit-3.4.2.4\\python-3.4.2.amd64\\lib\\site-packages\\win32',
'C:\\WinPython-64bit-3.4.2.4\\python-3.4.2.amd64\\lib\\site-packages\\win32\\lib',
'C:\\WinPython-64bit-3.4.2.4\\python-3.4.2.amd64\\lib\\site-packages\\Pythonwin',
'C:\\WinPython-64bit-3.4.2.4\\python-3.4.2.amd64\\lib\\site-packages\\IPython\\extensions']
So you need to provide a complete path or append the path to where the csv resides to your sys path. Note that backslashes must be escaped e.g. 'c:\\data\\my.csv' but if you use forward slashes then it works fine: e.g. 'c:/data/my.csv'