From: <kco...@cf...> - 2002-12-06 16:12:28
|
Hi, here I have a very simple code snippet to unzip a zip file. I'm working with Jython 2.1 and Python 2.2. The code works fine running in python but fails in jython. Jython is not detecting the os.path.basename(name)check and tries to open everything in zipfile. ****CODE***** import zipfile import os import string #open the zipped file and extract the content in c:\ zf =zipfile.ZipFile("C:\\temp\\apache-2_0_39-windows.zip", "r") if hasattr(zf, 'namelist'): files = zf.namelist() #print files for name in files: if os.path.basename(name): newFile = open ('C:/'+name, "wb") print newFile newFile.write (zf.read (name)) newFile.close() else: print "Making Directory", name os.makedirs('C:/'+ name) else: print "Zip File is empty" C:\Projects>java org.python.util.jython unzip_python.py Traceback (innermost last): File "unzip_python.py", line 12, in ? IOError: File not found - C:\apache\2.0.39 (The system cannot find the path spec ified) I tried changing the jython directory check to a if os.path.isfile(name): This fails too, it creates everything as a directory. PLEASE HELP> thx KC |
From: David R. <dr...@ri...> - 2002-12-07 00:27:50
|
kco...@cf... wrote: > here I have a very simple code snippet to unzip a zip file. > I'm working with Jython 2.1 and Python 2.2. The code works fine running in > python but fails in jython. > Jython is not detecting the os.path.basename(name)check and tries to open > everything in zipfile. ... > I tried changing the jython directory check to a > if os.path.isfile(name): > This fails too, it creates everything as a directory. Java often tries to enforce portability by only exposing a lowest common denominator view of the OS. So much of the Python os package is difficult or impossible to implement in Jython. basename() is tricky to get right in a "100% Pure" manner because Java thinks there's only one valid directory path separator per platform. isfile() can't really work perfectly because Java doesn't provide a way to tell a file from a link. Try using os.path.isdir() instead. If all else fails you can do it the Java way instead of the Python way using the java.util.zip package (high-level) or java.io.File (low-level). Of course, that code would be non-portable to CPython, unless you switched implementations based on whether os.name started with 'java' (Yuck.) -- David Ripton dr...@ri... |