Link to home
Start Free TrialLog in
Avatar of Isaiah Melendez
Isaiah Melendez

asked on

Python 3.5.2 - Script Help

Enviorment: Windows
Python: v3.5.2

Hi, Experts,

I am building a python file that works right now that grabs files from an URL and stores them into a variable.

Example: url1 = 'http://test.com/pathtofile/file.extension' (note: extension name does not matter here just explaining the logic).

I then have a section in the script that creates folders in a directory by doing a condition checker. If the directory exists, do nothing, otherwise, create the folder.

Lastly, I have a command that calls upon the web request > reads the variable > downloads the file and places them in the designated directory.

with urllib.request.urlopen(url1) as response, open(fileName1, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

Open in new window


Is there an efficient way to improve it? Also, how can I call upon those downloaded wheel files and have pip install them all through my script?

I will post the code below.

import urllib.request, shutil, os, subprocess

#these are the URL paths to get the files from the HTTP site
url1 = 'http://files.cbapps.com/pythoninstall/CBADE.py'
url2 = 'http://files.cbapps.com/pythoninstall/email_test.py'
url3 = 'http://files.cbapps.com/pythoninstall/PythonSales-9efe188d4be3.json'
url4 = 'http://files.cbapps.com/pythoninstall/ftp_live_test.py'
url5 = 'http://files.cbapps.com/pythoninstall/ftp_test.py'
url6 = 'http://files.cbapps.com/pythoninstall/requirements.txt'

#these are the url for the wheel files
urlwheels1 = 'http://files.cbapps.com/pythoninstall/wheels/cffi-1.9.1-cp35-cp35m-win32.whl'
urlwheels2 = 'http://files.cbapps.com/pythoninstall/wheels/cryptography-1.7.2-cp35-cp35m-win32.whl'
urlwheels3 = 'http://files.cbapps.com/pythoninstall/wheels/et_xmlfile-1.0.1-py3-none-any.whl'
urlwheels4 = 'http://files.cbapps.com/pythoninstall/wheels/google_api_python_client-1.6.2-py2.py3-none-any.whl'
urlwheels5 = 'http://files.cbapps.com/pythoninstall/wheels/httplib2-0.9.2-py3-none-any.whl'
urlwheels6 = 'http://files.cbapps.com/pythoninstall/wheels/idna-2.2-py2.py3-none-any.whl'
urlwheels7 = 'http://files.cbapps.com/pythoninstall/wheels/jdcal-1.3-py3-none-any.whl'
urlwheels8 = 'http://files.cbapps.com/pythoninstall/wheels/oauth2client-4.0.0-py2.py3-none-any.whl'
urlwheels9 = 'http://files.cbapps.com/pythoninstall/wheels/openpyxl-2.4.0-py2.py3-none-any.whl'
urlwheels10 = 'http://files.cbapps.com/pythoninstall/wheels/paramiko-2.1.1-py2.py3-none-any.whl'
urlwheels11 = 'http://files.cbapps.com/pythoninstall/wheels/pyasn1-0.2.2-py2.py3-none-any.whl'
urlwheels12 = 'http://files.cbapps.com/pythoninstall/wheels/pycparser-2.16-py2.py3-none-any.whl'
urlwheels13 = 'http://files.cbapps.com/pythoninstall/wheels/pyodbc-3.0.10-cp35-none-win32.whl'
urlwheels14 = 'http://files.cbapps.com/pythoninstall/wheels/pysftp-0.2.8-py3-none-any.whl'
urlwheels15 = 'http://files.cbapps.com/pythoninstall/wheels/pytz-2016.10-py2.py3-none-any.whl'
urlwheels16 = 'http://files.cbapps.com/pythoninstall/wheels/rsa-3.4.2-py2.py3-none-any.whl'
urlwheels17 = 'http://files.cbapps.com/pythoninstall/wheels/setuptools-28.6.1-py2.py3-none-any.whl'
urlwheels18 = 'http://files.cbapps.com/pythoninstall/wheels/setuptools-28.7.0-py2.py3-none-any.whl'
urlwheels19 = 'http://files.cbapps.com/pythoninstall/wheels/six-1.10.0-py2.py3-none-any.whl'
urlwheels20 = 'http://files.cbapps.com/pythoninstall/wheels/uritemplate-3.0.0-py2.py3-none-any.whl'
urlwheels21 = 'http://files.cbapps.com/pythoninstall/wheels/pyasn1_modules-0.0.8-py2.py3-none-any.whl'

#these are the folders that the os lib looks at and tests the condition to see if they exist
folderCreate1 = 'C:\CBASYNC'
folderCreate2 = 'C:\CBASYNC\install'
folderCreate3 = 'C:\CBASYNC\install\wheels'

#these are the files being saved into the appropiate locations
fileName1 = 'C:\\CBASYNC\\install\\CBADE.py'
fileName2 = 'C:\\CBASYNC\\install\\email_test.py'
fileName3 = 'C:\\CBASYNC\\install\\PythonSales-9efe188d4be3.json'
fileName4 = 'C:\\CBASYNC\\install\\ftp_live_test.py'
fileName5 = 'C:\\CBASYNC\\install\\ftp_test.py'
fileName6 = 'C:\\CBASYNC\\install\\requirements.txt'

#these are the files being saved into appropiate location for wheels
fileName7 = 'C:\\CBASYNC\\install\\wheels\\cffi-1.9.1-cp35-cp35m-win32.whl'
fileName8 = 'C:\\CBASYNC\\install\\wheels\\cryptography-1.7.2-cp35-cp35m-win32.whl'
fileName9 = 'C:\\CBASYNC\\install\\wheels\\et_xmlfile-1.0.1-py3-none-any.whl'
fileName10 = 'C:\\CBASYNC\\install\\wheels\\google_api_python_client-1.6.2-py2.py3-none-any.whl'
fileName11 = 'C:\\CBASYNC\\install\\wheels\\httplib2-0.9.2-py3-none-any.whl'
fileName12 = 'C:\\CBASYNC\\install\\wheels\\idna-2.2-py2.py3-none-any.whl'
fileName13 = 'C:\\CBASYNC\\install\\wheels\\jdcal-1.3-py3-none-any.whl'
fileName14 = 'C:\\CBASYNC\\install\\wheels\\oauth2client-4.0.0-py2.py3-none-any.whl'
fileName15 = 'C:\\CBASYNC\\install\\wheels\\openpyxl-2.4.0-py2.py3-none-any.whl'
fileName16 = 'C:\\CBASYNC\\install\\wheels\\paramiko-2.1.1-py2.py3-none-any.whl'
fileName17 = 'C:\\CBASYNC\\install\\wheels\\pyasn1-0.2.2-py2.py3-none-any.whl'
fileName18 = 'C:\\CBASYNC\\install\\wheels\\pycparser-2.16-py2.py3-none-any.whl'
fileName19 = 'C:\\CBASYNC\\install\\wheels\\pyodbc-3.0.10-cp35-none-win32.whl'
fileName20 = 'C:\\CBASYNC\\install\\wheels\\pysftp-0.2.8-py3-none-any.whl'
fileName21 = 'C:\\CBASYNC\\install\\wheels\\pytz-2016.10-py2.py3-none-any.whl'
fileName22 = 'C:\\CBASYNC\\install\\wheels\\rsa-3.4.2-py2.py3-none-any.whl'
fileName23 = 'C:\\CBASYNC\\install\\wheels\\setuptools-28.6.1-py2.py3-none-any.whl'
fileName24 = 'C:\\CBASYNC\\install\\wheels\\setuptools-28.7.0-py2.py3-none-any.whl'
fileName25 = 'C:\\CBASYNC\\install\\wheels\\six-1.10.0-py2.py3-none-any.whl'
fileName26 = 'C:\\CBASYNC\\install\\wheels\\uritemplate-3.0.0-py2.py3-none-any.whl'
fileName27 = 'C:\\CBASYNC\\install\\wheels\\pyasn1_modules-0.0.8-py2.py3-none-any.whl'

#C:\Program Files (x86)\Python35-32\Scripts - for pip install
#everything in the wheels/ folder is .WHL
#pythonsales is a .JSON
#files to save in c:\cbasync\install and \wheels

#STEP1:os lib condition checker to see if folders exist, if not, creates
if not os.path.exists(folderCreate1):
    os.makedirs(folderCreate1) 

if not os.path.exists(folderCreate2):
    os.makedirs(folderCreate2)   

if not os.path.exists(folderCreate3):
    os.makedirs(folderCreate3)
    

#STEP2:this goes and downloads the files from the URL and puts them into the dest location
with urllib.request.urlopen(url1) as response, open(fileName1, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(url2) as response, open(fileName2, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(url3) as response, open(fileName3, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(url4) as response, open(fileName4, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)    

with urllib.request.urlopen(url5) as response, open(fileName5, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(url6) as response, open(fileName6, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)
 
with urllib.request.urlopen(urlwheels1) as response, open(fileName7, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels2) as response, open(fileName8, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels3) as response, open(fileName9, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels4) as response, open(fileName10, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels5) as response, open(fileName11, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels6) as response, open(fileName12, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels7) as response, open(fileName13, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)                             

with urllib.request.urlopen(urlwheels8) as response, open(fileName14, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels9) as response, open(fileName15, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels10) as response, open(fileName16, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels11) as response, open(fileName17, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels12) as response, open(fileName18, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels13) as response, open(fileName19, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels14) as response, open(fileName20, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels15) as response, open(fileName21, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels16) as response, open(fileName22, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels17) as response, open(fileName23, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels18) as response, open(fileName24, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels19) as response, open(fileName25, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels20) as response, open(fileName26, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)

with urllib.request.urlopen(urlwheels21) as response, open(fileName27, 'wb') as out_file:
    shutil.copyfileobj(response,out_file)
   
#STEP3: install httplib2 - this comes from pip location in /scripts folder

#STEP4: install CBADE.py inside of c:\cbasync\install


#STEP5: install pip install wheels    


    

Open in new window

Avatar of aikimark
aikimark
Flag of United States of America image

put the (url, filename) tuples in a list of sets.  Then iterate through them.
Avatar of Isaiah Melendez
Isaiah Melendez

ASKER

what would be an example, aikimark?
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial