saving and loading files in python

does anyone here know how to save and load files to your harddrive- I am creating a script that needs to create, write to, save/ and ultimately load text files to and from the harddrive

this might get you started:

open a file in read-only mode:

file = open("filepath",r)
#read each line in a file, one at a time:
for line in file:
 print line #or whatever

#when you're done:

#you can also open a file in writeable mode:
file = open("filepath",w)
#write to the file:
#if you want to create a new line:
") #it's the same as pressing enter on your keyboard when writing a text
#don't forget to close the file when you're done:
#you could also open the file in append mode with an <b>a</b> instead of w. The behaviour is the same but with w you erase whatever was in the file. With a you write in the end of the file (continue writing data)

thanks for the info guys, that helps a lot! one more thing- now that I can write to a file, how do I access that file open/ file close dialogue in blender- you know the one that allows you to visually navigate the directory structure- just like when you open a .blend or save a .jpg or whatever

hey, ave… Have you read the Blender API documentation? If NOT, pls do this here for Blender 2.48 or here for Blender 2.49 - more than 99% both are the same… And after reading everything you may think is related to your issue, ask the question again here :wink:

Just find some basic issues yourself, ok?

This is the way of learning… I think… :slight_smile:

I use the API a lot, but havent managed to figure this one out yet

def function(filepath):
 print 'The selected file was:', filepath


@ ave - it is not a matter that you’re using (or not) the API but if you’re reading (or not) the API help… Go to the help for API, enter in the help for module Window and the very first example (on TOP of ALL) concerns exactly what you’re asking… Implemented in the Alberto’s code too! :wink:


Thanks albertoEAF - appreciate it

Abidos- aaaaaah I see, theres the little bugger… im still trying to find my way around the API, learn where everything is stored and how blender “thinks”- only discovered the index about a week ago… ill get there one day hopefully :smiley: