[Tutor] FTP file transfer stops, no error given

Craig Jackson craig.r.jackson at gmail.com
Thu May 19 10:11:56 EDT 2016


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('<!DOCTYPE html><html><head><meta
charset="UTF-8"><title>Peckerwood Garden Docent Notes</title>\n')
htmlpage.write('<meta name="robots" content="noindex">')
htmlpage.write('<META NAME="ROBOTS" CONTENT="NOARCHIVE">')
htmlpage.write('<link rel=Stylesheet href="css/stylesheet.css"></head>\n')
htmlpage.write('<body>\n')
htmlpage.write('<table>\n')
htmlpage.write('<tr>\n')
htmlpage.write('<th class="uid">Unique ID</th>\n')
htmlpage.write('<th class="loc">Location</th>\n')
htmlpage.write('<th class="nam">Name</th>\n')
htmlpage.write('<th class="fam">Family</th>\n')
htmlpage.write('<th class="com">Common Name</th>\n')
htmlpage.write('<th class="nat">Nativity</th>\n')
htmlpage.write('<th class="pho">Photographs</th>\n')
htmlpage.write('<th class="des">Description</th>\n')
htmlpage.write('</tr>\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('<tr>\n')
    htmlpage.write('<td class="uid">' + uniqueid + '</td>\n')
    htmlpage.write('<td class="loc">' + location + '</td>\n')
    htmlpage.write('<td class="nam">' + name + '</td>\n')
    htmlpage.write('<td class="fam">' + family + '</td>\n')
    htmlpage.write('<td class="com">' + commonname + '</td>\n')
    htmlpage.write('<td class="nat">' + nativity + '</td>\n')
    htmlpage.write('<td class="pho">')
    for x in glob.glob(imagesdirectory + uniqueid + '*'):
        htmlpage.write ('<a href=\"images/' + os.path.basename(x) +
'\">Photo</a>\n')
    htmlpage.write('</td>')
    htmlpage.write('<td class="des">' + description +'</td></tr>\n')
htmlpage.write('</table></body></html>')
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:

<a href="images/10863 s1700065 2014-nov.jpg">Photo</a>
<a href="images/10863 s1700080 2014-nov.jpg">Photo</a>
</td><td class="des"></td></tr>
</table></body></html>


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:

<a href="images/10863 2016-04-16 11.57.02.jpg">Photo</a>
<a href="images/10863 2016-04-16 11.58.01.jpg">Photo</a>
<a href="ima

As you can see, the upload terminated with no error. It happens 100%
of the time. It's interesting that ftp does not terminate
stylesheet.css.

If I run the scripts separately, the upload works fine.

Thanks for your help.
Craig Jackson


More information about the Tutor mailing list