Ad Code

Search, Copy and Compress Files - Python

  In this page, I'm sharing the model scripts and the details in which scenario it can be used.


It's not limited to given scenario, we can manipulate the scripts based on the requirements.

Make sure you are testing these scripts in your test environment, before using it on production.

======================================================== 

Scenario 3:

File type argument should be passed while executing the script.

Create the new folder based on the current date and time.

Search a file in a bucket or local path based on the input like pdf or png and filename in a input file, and copy the file to the Destination bucket or local path.

When all files are copied to the folder, the same folder will be compressed as .zip file, and give the path from where it can be downloaded.


Input: File names in the File.

File 1 - Input.txt ==> Add File names in this file

File 2 - Script.py ==> Python script is written in this file

Pre-requisite: If  the new folder should be created on storage bucket, use the respective command to create folder or mount it on the server and use the given Linux command to create folder.

How to execute the script: python script.py pdf


import os

import sys

import shutil

import datetime

now = datetime.datetime.now()

year = '{:02d}'.format(now.year)

month = '{:02d}'.format(now.month)

day = '{:02d}'.format(now.day)

hour = '{:02d}'.format(now.hour)

minute = '{:02d}'.format(now.minute)

second = '{:02d}'.format(now.second)

day_month_year = '{}_{}_{}_{}_{}_{}'.format(year, month, day, hour, minute, second)

foldername = "/bucketname/foldername/" + day_month_year

os.mkdir(foldername,0740)

folder1 = "%s"%(foldername.strip("/bucketname/foldername/"))

print("foldername:", folder1,"created")

f = open("input.txt","r")

i = sys.argv[1]

if i == "png":

        a = ".png"

if i == "pdf":

        a = ".pdf"

for x in f:

        filename = "%s*%s"%(x.strip('\n'),a)

        print(filename)

#break

        os.system((("gsutil -m cp -r gs://bucketname/sourcefolder/%s gs://bucketname/destinationfolder/%s/")%(filename,folder1)))

        os.system((("gsutil -m cp -r gs:///bucketname/sourcefolder/%s gs://bucketname/destinationfolder/%s/")%(filename,folder1)))

def zip(folder_name):

     out=shutil.make_archive(folder_name, 'zip', folder_name)

zip(foldername)

print(foldername+".zip created")

======================================================== 

Reactions

Post a Comment

0 Comments