[Pythonmac-SIG] File path problem (long post)

Jack Jansen Jack.Jansen at cwi.nl
Mon Jun 16 11:44:28 EDT 2003


You are actually getting the same behavior interactively and  
non-interactively, it is
just not what you expect, apparently.

os.path.join(a, b) has logic somewhat similar to:
     if isabs(b):
	    return b
     else:
         return a + ':' + b
The details are more involved (see :Lib:macpath.py for the  
implementation), but basically if b is already absolute it is returned  
as-is.

What you need to do is something like
     # Strip off the inpath part of the pathname
     assert(file[:len(self.cms.inpath)] == self.cms.inpath)
     partial_path = file[len(self.cms.inpath):]
     # Make sure its relative
     if partial_path[0] != ':':
         partial_path = ':' + partial_path
     # make corresponding output path
     opath = os.path.join(self.cms.outpath, partial_path)
     # Give it the correct extension
     opath = os.path.splitext(opath)[0]+'.html'

On Sunday, Jun 15, 2003, at 06:26 Europe/Amsterdam, Josh English wrote:

> The Problem so far.
>
> This has to do with translating a bunch of xml files into html. There  
> is
> a project folder Zaphod:Web (Zaphod is not my boot partition. Marvin  
> is,
> and Python is installed in Marvin). Inside the Zaphod:Web folder there
> is a folder called "input", so it's path name is 'Zaphod:Web:input:'  
> and
> inside this folder I have subfolders containing xml files. Here is the
> files being read:
>
> Zaphod:Web:input:code:pbm:pbmdoc.xml
> Zaphod:Web:nput:code:webmenu:webmenudd.xml
>
> The program reads the file at Zaphod:Web:cms.ini which has to  
> pertinent entries:
> inpath = input
> outpath = output
>
> Basically, the program reads the files in Zaphod:Web:input and should
> process each file into HTML and put them in a similar file directory
> under the Zaphod:Web:output directory (which does exist and is created
> by the program if needed). The files I am looking to get should have  
> the paths:
>
> Zaphod:Web:output:code:pbm:pbmdoc.html
> Zaphod:Web:output:code:webmenu:webmenudd.html
>
> Now here's the sticky bit: In the Python Interactive window I can get
> the following:
>
>>>> os.path.join('Zaphod:Wev:xml:code:pbm:pbmdoc.xml','Zaphod:Web:output 
>>>> :code:pbm:pbmdoc.html')
> 'Zaphod:Web:output:code:pbm:pbmdoc.html'
>>>> os.path.join('Zaphod:Web:output:code:pbm,pbmdoc.html','Zaphod:Web:in 
>>>> put:code:pbm:pbmdoc.xml')
> 'Zaphod:Web:input:code:pbm:pbmdoc.xml'
>
> Typo's notwithstanding, the first example is what I want to get.
>
> Now in my code the class object doing all of the work is called
> SiteUpdater, initialized as
>
> mycms = SiteUpdater(1,1)
> The parameters simply tell the program to process every file in the
> input directory (and subdirectories) and write a report to the standard
> output. Here are the four lines of code where I think my problem lies:
>
> print file,self.cms.outpath
> opath = os.path.join(file,self.cms.outpath)
> opath = os.path.splitext(opath)[0]+'.html'
> print "Updating",os.path.join(self.cms.inpath,file),"to",opath
> self.UpdatePage(os.path.join(self.cms.inpath,file),opath)
>
> Here is what I get on my report:
>
> Zaphod:Web:input:code:pbm:pbmdoc.xml Zaphod:Web:output
> Updating Zaphod:Web:input:code:pbm:pbmdoc.xml to  
> Zaphod:Web:input:code:pbm:pbmdoc.html
> Zaphod:Web:input:code:webmenu:webmenudd.xml Zaphod:Web:output
> Updating Zaphod:Web:input:code:webmenu:webmenudd.xml to  
> Zaphod:Web:input:code:webmenu:webmenudd.html
> SiteUpdater Report -- Processing  
> Zaphod:Web:input:code:pbm:pbmdoc.xml...
> ...Done.
> Processing Zaphod:Web:input:code:webmenu:webmenudd.xml...
> ...Done.
>
> What's happening is that the html files are being written to the same
> subdirectory as the xml input file sits. As far as I can see I should
> get the same behaviour that I got in the Python interactive session.
> When I reverse the second line of the sample code to:
>
> opath = os.path.join(self.cms.outpath,file)
>
> My report comes out to:
>
> Zaphod:Web:input:code:pbm:pbmdoc.xml Zaphod:Web:output
> Updating Zaphod:Web:input:code:pbm:pbmdoc.xml to  
> Zaphod:Web:input:code:pbm:pbmdoc.html
> Zaphod:Web:input:code:webmenu:webmenudd.xml Zaphod:Web:output
> Updating Zaphod:Web:input:code:webmenu:webmenudd.xml to  
> Zaphod:Web:input:code:webmenu:webmenudd.html
> SiteUpdater Report -- Processing  
> Zaphod:Web:input:code:pbm:pbmdoc.xml...
> ...Done.
> Processing Zaphod:Web:input:code:webmenu:webmenudd.xml...
> ...Done.
>
> Hey, Now it's exhibiting the exact same behaviour as before. Earlier I
> was getting every file to be output to Zaphod:Web:output.html,
> overwriting the output file for every input file generated.
>
> I'm obviously being blind to this problem. I'm running Python 2.2.2 on
> Mac OS 9.1, which is the latest I have been able to get to run
> successfully on this machine (all sorts of problems with 9.2 for some  
> reason).
>
> Can anyone spot what's going on here?
>
> Thanks,
> Josh English
> english at spiritone.com
>
> _______________________________________________
> Pythonmac-SIG maillist  -  Pythonmac-SIG at python.org
> http://mail.python.org/mailman/listinfo/pythonmac-sig
>
--
Jack Jansen, <Jack.Jansen at cwi.nl>, http://www.cwi.nl/~jack
If I can't dance I don't want to be part of your revolution -- Emma  
Goldman




More information about the Pythonmac-SIG mailing list