On Mon, Aug 16, 2010 at 8:51 PM, Mike Dewhirst <mi...@dewhirst.com.au> wrote: > On 16/08/2010 10:22pm, Doug Blank wrote: >> >> On Mon, Aug 16, 2010 at 7:23 AM, Mark Mooij<markmo...@gmail.com> wrote: >>> >>> Allright I tried a couple of things with your suggested appraoch: >>> > > <snip> > >> >> I doubt it. You shouldn't really do this: >> >> kmlpath = "%s/%s" % (basepath, game_id) > > I suggested this to avoid having to cast game_id as a string. I'm not all > that knowledgeable about the upcoming Python 3 tsunami but I figure there > will be less hassle if I use the inbuilt string formatting in %s. At least I > hope so! > >> >> but do this: >> >> kmlpath = os.path.join(basepath, game_id) > > Now I have a question - more Python than Django. os.path.join is very clever > but in Windows we have multiple approaches. One is to simply use forward > slashes and rely on the infrastructure to know what to do and the other is > to be agnostically cross-platform with os.path.sep and os.path.join. We can > also use r'raw\backslashes' and u'C:\\escaped\\backslashes' > > I have come unstuck using all of them at one time or another. I can't quite > put my finger on it at the moment but I have also seen things like > 'C:\path\to/some/file.txt' arising from combinations of the approaches and > surprisingly being successful. > > So my question is, has anyone written a dissertation on the best approaches > for particular circumstances?
Always use os.path.join and you will (almost) never go wrong, and will work cross-platform. If you need to make something into a string, use: kmlpath = os.path.join(basepath, str(item1), str(game_id), str(subthing)) One caveat is that the encoding of a string versus the file system encoding can differ, so you might need something different than str or unicode. But that doesn't sound like it is your original problem. -Doug > Mike > >> >> as different OS's have different path formats. Or perhaps it is the >> kml string that you are creating that has other issues (unicode?) I >> would print out the mkdirs string to see what you are creating. >> >> HTH, >> >> -Doug >> >>> Mark >>> >>> On 16 aug, 08:32, Mike Dewhirst<mi...@dewhirst.com.au> wrote: >>>> >>>> On 16/08/2010 12:57pm, Mike Dewhirst wrote: >>>> >>>> >>>> >>>> >>>> >>>>> On 16/08/2010 1:06am, Mark Mooij wrote: >>>>>> >>>>>> Hi Mike, >>>> >>>>>> Thanks for your reply. I haven't changed anything else, I am >>>>>> processing the MS updates, but I don't think this is the problem, as >>>>>> this all worked fine before the migration to 1.2.1, also if I call the >>>>>> script directly (outside Django) it works fine. >>>>>> My view looks like this: >>>>>> .. >>>>>> import createKML >>>>>> .. >>>>>> def my_view(request, arg): >>>>>> ... >>>>>> createKML.createKML(arg) >>>> >>>>> I don't really know what is happening here that might have changed in >>>>> 1.2.1 maybe someone else can suggest something. >>>> >>>>> In the meantime here is suggested approach to discovering the exact >>>>> problem ... >>>> >>>>> import os >>>>> ... >>>>> def createKML(id, basepath="D:/path/to/mysite/templates/maps"): >>>>> ... >>>>> try: >>>>> if id: >>>>> kmlpath = "%s/%s" % (basepath, str(id)) >>>> >>>> actually .. kmlpath = "%s/%s" % (basepath, id) >>>> >>>>> if not os.path.isdir(kmlpath): >>>>> os.makedirs(kmlpath) >>>>> writefile = "%s/%s" % (kmlpath, str(id)) >>>> >>>> actually .. writefile = "%s/%s" % (kmlpath, id) >>>> >>>> >>>> >>>>> f = open(writefile, 'wb') >>>>> else: >>>>> raise >>>>> except Exception as e: >>>>> print("id = %s\nbasepath = %s\n%s" % (id, basepath, e) >>>>> ... >>>> >>>>> mike >>>> >>>>>> ... >>>> >>>>>> In the createKML.py the problem arises in: >>>>>> ... >>>>>> import os >>>>>> ... >>>>>> def createKML(id): >>>>>> ... >>>>>> os.mkdir("D:/path/to/mysite/templates/maps/"+str(id)) >>>>>> writefile = "D:/path/to/mysite/templates/maps/"+str(id)+"/"+str(id) >>>>>> + ".kml" >>>>>> f = open(writefile, 'wb') >>>>>> ... >>>> >>>>>> I hope this helps? >>>> >>>>>> On 14 aug, 21:55, Mike Dewhirst<mi...@dewhirst.com.au> wrote: >>>>>>> >>>>>>> On 15/08/2010 12:10am, Mark Mooij wrote: >>>> >>>>>>>> Hi all, >>>> >>>>>>>> I recently migrated from Django 1.1.1 to 1.2.1. In 1.1.1 I had an >>>>>>>> application which imports an external python script in which a >>>>>>>> directory and some files are created. Since the migrate this doesn't >>>>>>>> work anymore, I get a number of IOErrors because the files (and >>>>>>>> directory) that should be created can't be found. I've tried >>>>>>>> changing >>>>>>>> my directories and slashes (I'm using Windows), but no success. >>>> >>>>>>> Have you changed anything else? >>>> >>>>>>> For example, are you processing the Microsoft Windows update patches >>>>>>> which (for WinXP) insert extra permission steps? Have you changed >>>>>>> your >>>>>>> OS from XP to Win7? >>>> >>>>>>> Can you post the source of the script? >>>> >>>>>>>> Can't I use the os python functions anymore in Django 1.2.1? In the >>>>>>>> documentation I've read about Managing files in models but I prefer >>>>>>>> to >>>>>>>> handle this through my external script. >>>> >>>>>>>> I hope anybody can help me. >>>> >>>>>>>> Thanks, >>>>>>>> Mark >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Django users" group. >>> To post to this group, send email to django-us...@googlegroups.com. >>> To unsubscribe from this group, send email to >>> django-users+unsubscr...@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/django-users?hl=en. >>> >>> >> > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.