Python load files from list. floader module is complete!

Question:

Ok, I’m writing a script for python to cut down a ton of logic bricks by loading scripts formed in a list. Now how would I go about doing this. XML?

Basically it needs to load up a file (idk if a dictionary would work or not) that has a list like so: item1.data, item2.data, item3.data. Now it needs to load the files individually. I know a workaround but it would require me to write both a hell of a lot of python and create 1 file dict for each one, and that’s just a pain. Any way I could just list all the files to load in 1 file, that would be amazing.

I need help asap.

Answer (self-solved ;))
Ok, it’s much more advanced now!

If you wish to download and or use floader look at our blog: http://mediachicken.com/blog/

The exact link to the floader tutorial is here: http://mediachicken.com/blog/blog.php?id=7

ENJOY GUYS!!!

Anyone know?

I need to know! Someone’s gotta know, I’m trying to cut down logic bricks!

Please provide more information/a clearer explanation of what you want. Most of the time around this forum if you don’t get a response at all, its because nobody understands what you’re asking…

I want to have a file called: list.data I want it to contain a list like so: file1.data, file2.data, file3.data. The directory tree is setup like this:

Folder ----- list.data,file1.data,file2.data,file3.data

Now when you run the python script you can load a new file in order like file4.data file5.data and I want to add an additional string to list.data “file4.data” now when I execute the python script I want it to load every file (file1.data - file4.data) that is listed in list.data

How can I do this? Would an XML file work better? This will be used so I won’t need 100 always___python I can load 1 python and load all my scripts from one. I will release when it’s done. (It’s for Blender Grape)

Would it not work to just have the list.data file be like:


file1.data
file2.data
file3.data
file4.data

And then when you read it in just do something like:


fo = open('list.data', 'r') # May need to put full path here
for nfile in fo.readlines():
     open(nfile, 'r') # May need to put a full path here
     # continue processing

Or am I still a little off-mark?

Why not just load all the files in a single directory? That way you don’t have to maintain an additional file. If you have new data file to load just put it in the directory and run the script.

Take a look at the “pickle” Python module - it allows you to skip a lot of the parsing business and just load Python data structures directly…

(Unless I totally mis-read what you’re trying to do)
RS

How would you load all files in a directory?

The quest for that begins with Google. Try asking Google just once and if you don’t find it, I’ll give you some more information.

http://www.python.org/doc/2.5.2/tut/node8.html

I’ve googled my questions many times, don’t assume I’m some big noob who comes to these forums to find out a python question without even researching. I searched a lot.

I got it working!!!

The list.data file contains the following:


file1.data
file2.data

when you run the python script it displays the contents of file1.data and file2.data with
separating them. Here’s the full script with comments to help you learn! I’d like to thank forTe for getting me on the right track, after looking further into the readlines() function I found linecache module.


import linecache
## the linecache module allows you to choose what line to read

count = 0
## this is the count on how many lines it printed

numlines = 0
for line in file('C:/test/list.data'): numlines += 1
## This fines out how many lines are in list.data
linecount = (numlines - 1)
## You have to delete 1 digit from linecount (because it
## loads 1 extra line for reasons I can't explain =X)


## VVVV This will keep looping the loading command until
## it runs out of lines to load
while count <= linecount:
    count += 1
    ## the count tell the script that is loaded 1 file
    lines = linecache.getline('C:/test/list.data', count)
    lines = lines.replace('
','')
    f = open(lines,'r')
    text = f.read()
    print text

oh and here’s the result of the script


this is test file 1
this is test file 2!!!

Updated first post with the new script and download!

I uploaded the floader module!! I hope you all enjoy! C&C is appreciated!

How can this benefit blender? Simple, it allows you to save your game in multiple files: ie, stats in > stats.data, location > xyz.data, etc. And load them all at one time!

Enjoy guys! Feel free to edit floader but DON’T steal and say you made it.

killer python lists start at item 0, not item 1, thats why you see -1 in many python programs. remember 0-9 all single didgets, not 1-10. you are going to run into the same thing very often. thats not a glitch, it’s the way it is suposed to work. if you tell python to get item 1 it is going to get the second item in the list. if you want to skip adding the minus just tell it to get item zero rather than item 1. if a list is a,b,c,d,e,f item 1 = b. a is item 0.

scratch that. i see what you did. you started at line 0 , remember 0 is the fist posistion. lo when it added your first line in the +1 that went into the second position. you list starts with an empty line, not the first line counted. 0 is a position, not an empty. when done it is giving you the total numbers of entries, and zero is an entry. or if that extra line of code really bugs you set numlines = -1

If you’re that concerned with people stealing it, you might want to license it (i.e. GPL)…

Its not airtight, but at least you would have a more concrete legal claim to it (right now you have nary a copyright statement in what you’ve posted in the thread…)

How do you get a gpl license?

They’re free and easy of course (though, please do make sure you understand the implications of an “open” license):

http://www.gnu.org/licenses/gpl-howto.html

-[killer]-

Just paste this below your header>


# your script name.py 
# Copyright your name 2008
# -------------------------------------------------------------------------- 
# ***** BEGIN GPL LICENSE BLOCK ***** 
# 
# This program is free software; you can redistribute it and/or 
# modify it under the terms of the GNU General Public License 
# as published by the Free Software Foundation; either version 2 
# of the License, or (at your option) any later version. 
# 
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
# GNU General Public License for more details. 
# 
# You should have received a copy of the GNU General Public License 
# along with this program; if not, write to the Free Software Foundation, 
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
# 
# ***** END GPL LICENCE BLOCK ***** 

--------------------------------------------------------------------------