Create files and directories from Django 1.2.1
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. 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.
Re: Create files and directories from Django 1.2.1
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) ... 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 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.
Re: Create files and directories from Django 1.2.1
Allright I tried a couple of things with your suggested appraoch: - I printed all the strings used to define paths and files, this all seems correct. - The os.mkdir() command is passed without raising an error, but without creating the directory either, this results in an "IOError: No such file or directory" because of course a file can't be created in a non-existing directory. - I tried creating files in different already existing directories and this works as expected. - I tried to use os.makedirs() instead of os.mkdir(), this raises an interesting error: "mkdir() takes exactly 1 argument (2 given)", I tried calling the script outside of Django and this should work the way I'm using it: kmlpath = "%s/%s" % (basepath, game_id) if not os.path.isdir(kmlpath): os.makedirs(kmlpath) - I tried creating a file without any directory structure in the path, the file is created in the root of the website, if I try creating a directory with os.mkdir() it should take the root as well I presume, but no directory is created. It seems that Django 1.2.1 has problems calling the os.mkdir() and makedirs() commands ? Mark On 16 aug, 08:32, Mike Dewhirst 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 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.
Re: Create files and directories from Django 1.2.1
Hi Doug, Thanks for your reply. I tried your suggestion: .. import createKML .. def createKML(id, basepath=os.getcwd()): ... try: kmlpath = os.path.join(basepath, "templates") kmlpath = os.path.join(kmlpath, "maps") kmlpath = os.path.join(kmlpath, str(id)) if not os.path.isdir(kmlpath): os.mkdir(kmlpath) writefile = os.path.join(kmlpath, (str(id)+".kml")) except Exception as e: ... f = open(writefile, 'wb') But no luck so far. using only os commands to define the directory path should exclude any OS path formats issues I suppose? If I print the paths they look just fine. I also tried hardcoding str(id) with a predefined string but this doesn't work either, same problems arise. Calling the script outside Django works fine (as did using the script from Django 1.1.1), Also I find it strange that makedirs command rises an error when calling from Django ("mkdir() takes exactly 1 argument (2 given)"), but again outside Django this works just fine.. Hope anybody has any suggestions.. Mark On 16 aug, 14:22, Doug Blank wrote: > On Mon, Aug 16, 2010 at 7:23 AM, Mark Mooij wrote: > > Allright I tried a couple of things with your suggested appraoch: > > > - I printed all the strings used to define paths and files, this all > > seems correct. > > > - The os.mkdir() command is passed without raising an error, but > > without creating the directory either, this results in an "IOError: No > > such file or directory" because of course a file can't be created in a > > non-existing directory. > > > - I tried creating files in different already existing directories and > > this works as expected. > > > - I tried to use os.makedirs() instead of os.mkdir(), this raises an > > interesting error: "mkdir() takes exactly 1 argument (2 given)", I > > tried calling the script outside of Django and this should work the > > way I'm using it: > > > kmlpath = "%s/%s" % (basepath, game_id) > > if not os.path.isdir(kmlpath): > > os.makedirs(kmlpath) > > > - I tried creating a file without any directory structure in the path, > > the file is created in the root of the website, if I try creating a > > directory with os.mkdir() it should take the root as well I presume, > > but no directory is created. > > > It seems that Django 1.2.1 has problems calling the os.mkdir() and > > makedirs() commands ? > > I doubt it. You shouldn't really do this: > > kmlpath = "%s/%s" % (basepath, game_id) > > but do this: > > kmlpath = os.path.join(basepath, game_id) > > 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 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 > >> >