[Tutor] File Writing Permission?

Eric Brunson brunson at brunson.com
Sun Oct 7 22:05:56 CEST 2007

David Millar wrote:
> Hi everyone,
> I'm working on making a Debian package for a game I'm working on, and 
> it works fine, but I've run into a problem with my script now. The 
> script is placed in /usr/bin so all users on the system have access, 
> but even though it will run fine, I can't save files to that 
> directory, so the saving feature doesn't work and the rest of my game 
> is crippled now. Any ideas on how I could progress? I tried appending 
> "~/" to the filename for that copy of the script, but it just makes 
> the terminal window close without warning. Any help would be appreciated.
> def savegame(fname):
>     # This part isn't necessary, I just wanted to see if it would save 
> to the user's home folder
>     if os.name <http://os.name> == "posix":
>         fname = "~/" + fname

Hi Dave,

Python doesn't immediately do tilde expansion on filenames, you have to 
ask for it explicitly.  Normally this is done by your shell, but since 
most modern shells do it, it's easy to assume it's an inherent property 
of filenames and not an added feature.

Simply change:

fname = "~/" + fname


fname = os.path.expanduser( os.path.join( "~", fname ) )

Hope that helps,
>     infile = open(fname,"w")
>     # Save the maps
>     mapchanges.append([6,1,rrmap])
>     mapchanges.append([6,2,treemap])
>     picklelist = [choins, inventory, book, mapchanges, event, cdata]
>     pickle.dump(picklelist,infile)
>     # Delete the saved maps because they will just get in the way later
>     del mapchanges[len(mapchanges)-1]
>     del mapchanges[len(mapchanges)-1]
>     infile.close()
> David Millar
> http://www.thegriddle.net/python/
> ------------------------------------------------------------------------
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list