Re: Using a Callback Function - ftplib
On Aug 18, 6:02 am, Nitebirdz nitebi...@sacredchaos.com wrote: On Mon, Aug 17, 2009 at 11:10:25AM -0700, seldan24 wrote: I didn't even notice the higher level methods. I changed the retrieval line to: ftp.nlst(testfile*.txt) This works great. The result is even captured in an array. I really have no idea what the difference between a LIST and NLST is within FTP. Never delved that deep into it. I did notice that an NLST will return a specific FTP code if a file doesn't exist, whereas a LIST doesn't. So, I ended up using NLST as that'll generate an ftplib.error_perm exception. Based on if the job cares if a file is not available or not (some do, some don't), I'll either exit, or continue on with the file loop. The following thread from a NetBSD mailing list may help clarify this issue: http://mail-index.netbsd.org/netbsd-users/2001/01/30/0016.html NLST returns a machine-readable list of names, while LIST returns a human-readable list. Hene the presence of the FTP code in the case of NLST. Nitebirdz, Thanks for the information. I knew it stood for 'named list' but had no idea how that differed from the standard. I appreciate the link. -- http://mail.python.org/mailman/listinfo/python-list
Using a Callback Function - ftplib
Hello, I'm utterly confused by something which is most likely trivial. I'm attempting to connect to an FTP server, retrieve a list of files, and store than in an array. I.e.: import ftplib ftp = ftplib.FTP(server) ftp.login(user, pass) ftp.cwd(conf['testdir']) ftp.retrlines('NLST ' + testfile*.txt) ftp.quit() The above example works fine... and would return a list of any files that match testfile*.txt to standard out. The issue is I don't want that to go to stdout, I'd rather capture them within an array so I can retrieve them later. If I try something like: my_files = ftp.retrlines('NLST ' + testfile*.txt) Then, my_files, will just print out the return code of the FTP NLST command. I'm trying to get the file names themselves. Now, I've read through the ftplib module section of the Python documentation and it says that, by default, the output goes to sys.stdout unless a callback function is used. Here is where I get utterly lost. I can certainly see the files being outputted to sys.stdout, but don't know how to capture that output... i.e. testfile1.txt testfile2.txt testfile3.txt Will show to the screen, but I can't catch it! I'm sure this is trivial... any help would be greatly appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using a Callback Function - ftplib
On Aug 17, 1:51 pm, David 71da...@libero.it wrote: Il Mon, 17 Aug 2009 10:43:33 -0700 (PDT), seldan24 ha scritto: Hello, I'm utterly confused by something which is most likely trivial. I'm attempting to connect to an FTP server, retrieve a list of files, and store than in an array. I.e.: import ftplib ftp = ftplib.FTP(server) ftp.login(user, pass) ftp.cwd(conf['testdir']) Why bother with retrlines? Use the provided higer level fuctions: remotefiles = [] ftp.dir(remotefiles.append) or, if you prefer nlst remotefiles = ftp.nlst() regards david I didn't even notice the higher level methods. I changed the retrieval line to: ftp.nlst(testfile*.txt) This works great. The result is even captured in an array. I really have no idea what the difference between a LIST and NLST is within FTP. Never delved that deep into it. I did notice that an NLST will return a specific FTP code if a file doesn't exist, whereas a LIST doesn't. So, I ended up using NLST as that'll generate an ftplib.error_perm exception. Based on if the job cares if a file is not available or not (some do, some don't), I'll either exit, or continue on with the file loop. Anyway, thanks again, works perfectly, next time I'll try to scroll down and read a bit more prior to posting! -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Equivalent for dd fold
On Jul 15, 1:48 pm, Emile van Sebille em...@fenx.com wrote: On 7/15/2009 10:23 AM MRAB said... On Jul 15, 12:47 pm, Michiel Overtoom mot...@xs4all.nl wrote: seldan24 wrote: what can I use as the equivalent for the Unix 'fold' command? def fold(s,len): while s: print s[:len] s=s[len:] snip You might still need to tweak the above code as regards how line endings are handled. You might also want to tweak it if the strings are _really_ long to simply slice out the substrings as opposed to reassigning the balance to a newly created s on each iteration. Emile Thanks for all of the help. I'm almost there. I have it working now, but the 'fold' piece is very slow. When I use the 'fold' command in shell it is almost instantaneous. I was able to do the EBCDIC-ASCII conversion usng the decode method in the built-in str type. I didn't have to import the codecs module. I just decoded the data to cp037 which works fine. So now, I'm left with a large file, consisting of one extremely long line of ASCII data that needs to be sliced up into 35 character lines. I did the following, which works but takes a very long time: f = open(ascii_file, 'w') while ascii_data: f.write(ascii_data[:len]) ascii_data = ascii_data[len:] f.close() I know that Emile suggested that I can slice out the substrings rather than do the gradual trimming of the string variable as is being done by moving around the length. So, I'm going to give that a try... I'm a bit confused by what that means, am guessing that slice can break up a string based on characters; will research. Thanks for the help thus far. I'll post again when all is working fine. -- http://mail.python.org/mailman/listinfo/python-list
Python Equivalent for dd fold
Hello, I have a shell script, that I'm attempting to convert to Python. It FTP's files down from an AS/400 machine. That part is working fine. Once the files arrive, the script converts them from EBCDIC to ASCII and then formats their line width based on a pre-determined size. For example, if I have the file TESTFILE and I know it should be formatted to 32 characters/line, I run: dd if=TESTFILE,EBCDIC conv=ASCII | fold -w 32 TESTFILE.ASCII Luckily, the files have the packed decimal format common in COBOL programs converted already prior to reaching me, so all I have to do is the conversion and line width formatting. The above works fine, and I know I could (and may) just embed it using subprocess.Popen. This, I've figured out how to do. But, ideally, I'd like to convert as much shell to native Python as possible, so that I can learn more. I think I can figure out the first part by using the codecs module... found some information on Google related to that. My question is, what can I use as the equivalent for the Unix 'fold' command? I don't really need to know how to do it, just a push in the right direction. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Equivalent for dd fold
On Jul 15, 12:47 pm, Michiel Overtoom mot...@xs4all.nl wrote: seldan24 wrote: what can I use as the equivalent for the Unix 'fold' command? def fold(s,len): while s: print s[:len] s=s[len:] s=A very long string indeed. Really that long? Indeed. fold(s,10) Output: A very lon g string i ndeed. Rea lly that l ong? Indee d. Greetings, -- The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing. - Vinod Valloppillilhttp://www.catb.org/~esr/halloween/halloween4.html Wow, I feel like a dork. I should have done more research prior to posting. Anyway, thanks for the advice. The trouble with Python is that things make 'too much' sense. Loving this language. -- http://mail.python.org/mailman/listinfo/python-list
Re: Best Way to Handle All Exceptions
On Jul 13, 10:33 am, MRAB pyt...@mrabarnett.plus.com wrote: seldan24 wrote: Hello, I'm fairly new at Python so hopefully this question won't be too awful. I am writing some code that will FTP to a host, and want to catch any exception that may occur, take that and print it out (eventually put it into a log file and perform some alerting action). I've figured out two different ways to do this, and am wondering which is the best (i.e. cleanest, 'right' way to proceed). I'm also trying to understand exactly what occurs for each one. The first example: from ftplib import FTP try: ftp = FTP(ftp_host) ftp.login(ftp_user, ftp_pass) except Exception, err: print err This works fine. I read through the documentation, and my understanding is that there is a built-in exceptions module in python, that is automatically available in a built-in namespace. Within that module is an 'Exception' class which would contain whatever exception is thrown. So, I'm passing that to the except, along with err to hold the value and then print it out. There isn't an exceptions module; exceptions are part of the language. The second example: from ftplib import FTP import sys try: ftp = FTP(ftp_host) ftp.login(ftp_user, ftp_pass) except: print sys.exc_info() This is called a bare exception handler. It's virtually never the right way to do it. Here I, for the most part, get the same thing. I'm not passing anything to except and just printing out the exception using a method defined in the sys module. So, I'm new to Python... I've made it this far and am happy, but want to make sure I'm coding correctly from the start. Which method is the better/cleaner/more standard way to continue? Thanks for any help. You should use the most specific exception possible because at lot of things could raise an exception: foo Traceback (most recent call last): File pyshell#0, line 1, in module foo NameError: name 'foo' is not defined try: foo except Exception, e: print *** Caught an exception *** print e *** Caught an exception *** name 'foo' is not defined- Hide quoted text - - Show quoted text - Thank you both for your input. I want to make sure I get started on the right track. For this particular script, I should have included that I would take the exception contents, and pass those to the logging module. For this particular script, all exceptions are fatal and I would want them to be. I just wanted a way to catch them and log them prior to program termination. I can understand why folks should always specify exact exceptions where possible if they plan on doing something with them, but I just want to log and terminate (and alarm) when any exception, irregardless of what it is, occurs; that's why I'm using the catch-all approach. I most likely should have put in an exit() in my snippet; sorry about that. I did notice that Python allows different objects to be thrown. This threw me off a bit because, at first, I was expecting everything to come through as nice, easy tuples. It turns out that ftplib throws some class from the sockets module (my terminology may be off here). As for terminology, sorry about the 'exceptions' misuse. The Python documentation refers to 'exceptions' as a module, albeit built-in, so I used that language accordingly (link to doc: http://docs.python.org/library/exceptions.html?highlight=exceptions#module-exceptions). Anyway, thanks again for the help and advice, it is greatly appreciated. -- http://mail.python.org/mailman/listinfo/python-list