The Python script has two objectives: a) create a web page from data
in an Excel spreadsheet, b) upload that web page to a web server using
ftp. In a nutshell the problem is that if the two objectives are
combined into one Python script, ftp stops uploading in the middle of
the file, but if the same code is split into two files, web page
creation in one script and ftp in another, and they are run
separately, ftp uploads the entire file and it all works. Below are
the details.
Script #1: Create web page
import glob, os, sys, unicodedata
imagesdirectory = 'C:\\Users\\craig\\Desktop\\Network
Drives\\Documents\\Volunteer and Nonprofit\\Peckerwood Garden\\tour
guide information\\images\\'
spreadsheet='C:\\Users\\craig\\Desktop\\Network
Drives\\Documents\\Volunteer and Nonprofit\\Peckerwood Garden\\tour
guide information\\index2.xlsx'
# load excel file and set directory variables
from openpyxl import load_workbook
wb = load_workbook(filename = spreadsheet, data_only = True)
ws = wb.get_sheet_by_name('Garden')
numberofrecords = ws.cell(row = 1, column = 1).value
htmlpage = open('C:\\Users\\craig\\Desktop\\Network
Drives\\Documents\\Volunteer and Nonprofit\\Peckerwood Garden\\tour
guide information\\index2.html', 'w')
htmlpage.write('Peckerwood Garden Docent Notes\n')
htmlpage.write('')
htmlpage.write('')
htmlpage.write('\n')
htmlpage.write('\n')
htmlpage.write('\n')
htmlpage.write('\n')
htmlpage.write('Unique ID\n')
htmlpage.write('Location\n')
htmlpage.write('Name\n')
htmlpage.write('Family\n')
htmlpage.write('Common Name\n')
htmlpage.write('Nativity\n')
htmlpage.write('Photographs\n')
htmlpage.write('Description\n')
htmlpage.write('\n')
# loop through excel spreadsheet, search images directory, create html file
for i in range(2,numberofrecords + 2):
uniqueid = str(ws.cell(row = i, column = 2).value)
location = unicodedata.normalize('NFKD', ws.cell(row = i, column =
3).value).encode('ascii','ignore')
name = unicodedata.normalize('NFKD', ws.cell(row = i, column =
4).value).encode('ascii','ignore')
if ws.cell(row = i, column = 5).value:
family = unicodedata.normalize('NFKD', ws.cell(row = i, column
= 5).value).encode('ascii','ignore')
else:
family = ""
if ws.cell(row = i, column = 6).value:
commonname = unicodedata.normalize('NFKD', ws.cell(row = i,
column = 6).value).encode('ascii','ignore')
else:
commonname = ""
if ws.cell(row = i, column = 7).value:
nativity = unicodedata.normalize('NFKD', ws.cell(row = i,
column = 7).value).encode('ascii','ignore')
else:
nativity = ""
if ws.cell(row = i, column = 8).value:
description = unicodedata.normalize('NFKD', ws.cell(row = i,
column = 8).value).encode('ascii','ignore')
else:
description = ""
htmlpage.write('\n')
htmlpage.write('' + uniqueid + '\n')
htmlpage.write('' + location + '\n')
htmlpage.write('' + name + '\n')
htmlpage.write('' + family + '\n')
htmlpage.write('' + commonname + '\n')
htmlpage.write('' + nativity + '\n')
htmlpage.write('')
for x in glob.glob(imagesdirectory + uniqueid + '*'):
htmlpage.write ('Photo\n')
htmlpage.write('')
htmlpage.write('' + description +'\n')
htmlpage.write('')
htmlpage.close
Script #2: Use FTP to upload files
# open ftp and copy up web files
from ftplib import FTP
ftp = FTP('wildtrip.com','user','pass')
ftp.set_debuglevel(2)
ftp.cwd('/wildtrip.com/tour/css/')
filename = 'C:\\Users\\craig\\Desktop\\Network
Drives\\Documents\\Volunteer and Nonprofit\\Peckerwood Garden\\tour
guide information\\css\\stylesheet.css'
ftp.storlines("STOR stylesheet.css", open(filename, 'r'))
ftp.cwd('/wildtrip.com/tour/')
filename = 'C:\\Users\\craig\\Desktop\\Network
Drives\\Documents\\Volunteer and Nonprofit\\Peckerwood Garden\\tour
guide information\\index2.html'
ftp.storlines("STOR index.html", open(filename, 'r'))
ftp.quit()
Combined scripts have output as follows:
the last lines of the html file on the local disk:
Photo
Photo
As you can see the script correctly wrote the html code to the local file.
However, ftp upload of this very same local file terminates. The last
lines of the uploaded file:
Photo
Photo
https://mail.python.org/mailman/listinfo/tutor