Proper usage of Python 3 ftp class -


as python newbie still learning language, struggled couple of days trying simple ascii file transfer (stor/put) using ftp class in ftplib (running python 3.3).

after using storbinary() method , consistently getting typeerror: "type str doesn't support buffer api", discovered discussion on thread, implies there bug in port of ftplib python 3:

http://bugs.python.org/issue6822

i tried using storbinary() instead of storlines(), using file object opened using 'rb' switch , seems work perfectly. i'm working on windows system, , testing/learning purposes i'm uploading own site on linux host. after uploading both .zip , .txt files , copying them down workstation using filezilla, both files intact.

in day-to-day work need upload gzipped , ascii files mainframe, , concerned may leaving myself open file transfer errors using counter-intuitive work-around. i've screwed many manual ftp transfers when forgetting switch appropriate transfer mode, feels creepy able transfer both binary , ascii files using same code!

can comment on how i'm implementing library class?

thanks.

filename = 'f:\\data_folder\\test_file.txt' fileparts = os.path.split(filename) putfile = fileparts[1] cmd = 'stor {}'.format(putfile) fileobject = open(filename, 'rb') ftp.storbinary(cmd, fileobject)  filename = 'f:\\data_folder\\test_file.zip' fileparts = os.path.split(filename) putfile = fileparts[1] cmd = 'stor {}'.format(putfile) fileobject = open(filename, 'rb') ftp.storbinary(cmd, fileobject) 

6/28/2013 - coming here kinda "close loop" on issue. while can use open(filename, 'rb') ftp.storbinary() both binary , ascii text files, both windows , linux hosts target, when mainframe target, text file getting garbled, appearing binary file.

by adding switch wrapper class continue open file 'rb' argument, using storlines() instead transfer, file arrives @ destination intact. i'm willing bet there configuration options on mainframe side make behavior vary 1 host another, i'm hoping mentioning alert encountering thread possibility apparently "safe" combination of open(filename, 'rb') , storbinary() may not succeed ftp hosts, notably mainframe systems. may determined through trial-and-error, there cases in correct approach transferring ascii data require open(filename, 'rb') storlines().

the 3.3 documentation storlines explicitly says:

lines read until eof file object file (opened in binary mode)…

so, passing file opened in text mode isn't supposed work.

ftp's text (ascii) mode isn't same think python's text mode. in particular, ftp text required ascii (and real 7-bit ascii, not extended codepage values > 127). python text has explicitly-specified character set, , treated unicode. if files utf-8, latin-1, cp-850, etc., can't use text mode.

on top of that, both python , ftp allowed munge line endings text files. want that, can upload windows text file linux box , have show unix line-endings instead of windows (although may not happen, depending on variety of things…). otherwise, don't want use text mode.

in short, you're doing right thing opening text files in binary mode , uploading them in binary (image) mode.


meanwhile, code fine as-is, if you're looking ways improve it, there's room minor changes.

first, if you've got same 7 lines copied , pasted twice, , difference string in 1 line, factor out function.

also, close files. either add explicit fileobject.close(), or, better, use with statement. if you've got 2 files in short-lived script, won't make difference, it's still idea—and might later expand opens more 2 files or lives longer.

if want basename of file, it's clearer call basename call split , access [1].

getting nitpicky, unless you've got lots of "legacy" or wrapper code using different style, it's better stick pep 8 invent own style.

finally, if want leave open possibility of sending text , binary files differently, though @ present they're implemented same, write upload_binary_file , upload_text_file, , make latter call second, or reference same function. however, don't want this. reasons explained above, , in j.f. sebastian's comments, upload_text_file function more misleading attractive nuisance useful hook future expansion.

so:

def upload_file(filename):     put_file = os.path.basename(filename)     cmd = 'stor {}'.format(put_file)     open(filename, 'rb') file_object:         ftp.storbinary(cmd, file_object)  upload_file('f:\\data_folder\\test_file.txt') upload_file('f:\\data_folder\\test_file.zip') 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -