[Tutor] FTP file transfer stops, no error given

2016-05-19 Thread Craig Jackson
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


Re: [Tutor] FTP file transfer stops, no error given

2016-05-19 Thread Craig Jackson
Adding the parentheses worked. So sorry for taking your time. I'm new
to Python and not at all a programmer. Thanks for your help.

On Thu, May 19, 2016 at 11:37 AM, Alan Gauld via Tutor  wrote:
> On 19/05/16 15:11, Craig Jackson wrote:
>
>> 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.
>
> Unfortunately you seem to have sent the variation that works.
> We cannot really tell what is going on unless we can see the
> code that fails.
>
> There is one possibility based on the code below...
>
>> htmlpage.write('> charset="UTF-8">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')
>
> Consider using triple quoted strings to save yourself a lot of typing
> and to make the html easier to read.
>
>> ...
>> htmlpage.write('')
>> htmlpage.write('' + description +'\n')
>> htmlpage.write('')
>> htmlpage.close
>
> Note that there are no parens after close. So you do not execute the
> function and the file is not closed.
>
> If you then try to ftp the still open file that could explain the
> problems you are having. Whereas if you run this as a separate script
> the file will be auto-closed at the end of the script so running the
> ftp separately will work..
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor