Alice Community

Alice Community (http://www.alice.org/community/index.php)
-   Share Objects (http://www.alice.org/community/forumdisplay.php?f=8)
-   -   File I/O Tool (http://www.alice.org/community/showthread.php?t=6758)

arty-fishL 06-14-2011 06:15 PM

File I/O Tool
 
1 Attachment(s)
This is a neat file I/O tool I made for [URL="http://www.alice.org/community/member.php?u=18410"]TauTrumpsPi[/URL].
Import the object into the world you want to use it in.
Don't rename it and don't delete any of its variables, methods or events.

Set IOTool's variable named current_file to the file to be used. Change it when necessary.
Use any of its methods wherever required.
Some methods have basic help/info in comments inside them.
Any method that returns a value (eg read_all or get_lines) will write its value to IOTool's variable named returnString (or returnNumber for get_lines).

Update 1.1 - should work perfectly now.
Update 1.2 - now it actually definitely works; trialled and tested.
[B]
New Version Available - [URL="http://alice.org/community/showthread.php?p=45897"]Save Tool[/URL] - for specifically saving and loading variables[/B]



[B]Read below only if you require help:[/B]

[SIZE=1][I][B]get_value[/B][/I] - returns to [U][I]returnString[/I][/U] - parameter [U][I]value[/I][/U] is the name of the value key - returns info about the file (eg size).
[I][B]new_file[/B][/I] - returns [U]none[/U] - parameter [U][I]filename[/I][/U] is the complete folder, file,ext name (ie C:\Users\Me\Documents\blank.txt).
[I][B]new_folder[/B][/I] - returns [U]none[/U] - parameter [U][I]dirname[/I][/U] is the complete pathname of the directory (ie C:\Users\Me\Documents\folder).
[B]read_all[/B] - returns to [U][I]returnString[/I][/U] - [U]no[/U] parameters - reads the entire file.
[B]read_line[/B] - returns to [U][I]returnString[/I][/U] -parameter line is the line to read - reads only a given line from the file.
[B]get_lines[/B] - returns to [U][I]returnNumber[/I][/U] -[U]no[/U] parameters - returns the number of lines of text in the file.
[B]write[/B] - returns [U]none[/U] - parameter [U][I]text[/I][/U] is the string to write with - writes over everything in the file.
[B]write_line[/B] - returns [U]none[/U] - parameter [U][I]text[/I][/U] is the string to write with, [U][I]line[/I][/U] is the line to write over - writes over everything on only the specified line in the file.
[B]write_append[/B] - returns [U]none[/U] - parameter [U][I]text[/I][/U] is the string to write with - writes onto the end of the file, retaining any text already in it.
[B]delete_file[/B] - returns [U]none[/U] - [U]no[/U] parameters - completely removes the file from disc.
[B]replace_file[/B] - returns [U]none[/U] - parameter [U][I]file_to_overwrite_with[/I][/U] is the complete filename of the file to write with - replaces the current file with the contents of another file, replaces in binary, so can replace with images etc.
[B]rename_file[/B] - returns [U]none[/U] - parameter [U][I]name[/I][/U] is the new name to give the file, set [U][I]full_path[/I][/U] to true to change complete filename; or to false just to change name relative to the director[/SIZE]y it is in - renames the file.

[SIZE=2][B]Support me by using [URL="http://adf.ly/8J02s"][B][SIZE=2]this DOWNLOAD link[/SIZE][/B][/URL][/B] (AdF.ly)[/SIZE]

David B 06-14-2011 06:44 PM

[QUOTE=arty-fishL;37453]This is a neat file I/O tool I made for [URL="http://www.alice.org/community/member.php?u=18410"]TauTrumpsPi[/URL].
Import the object into the world you want to use it in.
Don't rename it and don't delete any of its variables, methods or events.

Set IOTool's variable named current_file to the file to be used. Change it when necessary.
Use any of its methods wherever required.
Some methods have basic help/info in comments inside them.
Any method that returns a value (eg read_all or get_lines) will write its value to IOTool's variable named returnString (or returnNumber for get_lines).





[B]Read below only if you require help:[/B]

[SIZE=1][I][B]get_value[/B][/I] - returns to [U][I]returnString[/I][/U] - parameter [U][I]value[/I][/U] is the name of the value key - returns info about the file (eg size).
[I][B]new_file[/B][/I] - returns [U]none[/U] - parameter [U][I]filename[/I][/U] is the complete folder, file,ext name (ie C:\Users\Me\Documents\blank.txt).
[I][B]new_folder[/B][/I] - returns [U]none[/U] - parameter [U][I]dirname[/I][/U] is the complete pathname of the directory (ie C:\Users\Me\Documents\folder).
[B]read_all[/B] - returns to [U][I]returnString[/I][/U] - [U]no[/U] parameters - reads the entire file.
[B]read_line[/B] - returns to [U][I]returnString[/I][/U] -parameter line is the line to read - reads only a given line from the file.
[B]get_lines[/B] - returns to [U][I]returnNumber[/I][/U] -[U]no[/U] parameters - returns the number of lines of text in the file.
[B]write[/B] - returns [U]none[/U] - parameter [U][I]text[/I][/U] is the string to write with - writes over everything in the file.
[B]write_line[/B] - returns [U]none[/U] - parameter [U][I]text[/I][/U] is the string to write with, [U][I]line[/I][/U] is the line to write over - writes over everything on only the specified line in the file.
[B]write_append[/B] - returns [U]none[/U] - parameter [U][I]text[/I][/U] is the string to write with - writes onto the end of the file, retaining any text already in it.
[B]delete_file[/B] - returns [U]none[/U] - [U]no[/U] parameters - completely removes the file from disc.
[B]replace_file[/B] - returns [U]none[/U] - parameter [U][I]file_to_overwrite_with[/I][/U] is the complete filename of the file to write with - replaces the current file with the contents of another file, replaces in binary, so can replace with images etc.
[B]rename_file[/B] - returns [U]none[/U] - parameter [U][I]name[/I][/U] is the new name to give the file, set [U][I]full_path[/I][/U] to true to change complete filename; or to false just to change name relative to the director[/SIZE]y it is in - renames the file.[/QUOTE]

Cool! It looks interesting!

TauTrumpsPi 06-14-2011 07:46 PM

I can't get it to work... It seems like you're missing an integer variable of some sort

Here's the error

[code]
Error during simulation.




Alice version: 2.2 02/10/2011



Throwable that caused the error:

Traceback (innermost last):

File "edu.cmu.cs.stage3.alice.core.property.ScriptProperty[name=script,owner=edu.cmu.cs.stage3.alice.core.response.ScriptResponse[world.iOTool.read_all.__Unnamed0__]]", line 1, in ?

NameError: fileinfo


[/code]

DensetsuNoKaboom 06-15-2011 07:29 AM

My head is spinning, partly because Arty just made a new manly batch of awesome, but also partly because I don't know what to do with it.

Can you write new .txt files with it?

arty-fishL 06-15-2011 12:09 PM

[QUOTE=TauTrumpsPi;37467]I can't get it to work... It seems like you're missing an integer variable of some sort

Here's the error
[/QUOTE]Give it a second to start before you use it, don't use it on world start.
If that doesn't work, do you have an [I]"on world start" [/I]event under the IOTool heading? If you don't then there should be one.

[QUOTE=DensetsuNoKaboom;37494]Can you write new .txt files with it?[/QUOTE]Yes, and read them. (they don't have to end in .txt, but they should be plain text files ie. not images etc.)

TauTrumpsPi 06-15-2011 01:10 PM

I got it! Thank you!

Chao 06-15-2011 01:12 PM

Thanks! This will be very useful!

DensetsuNoKaboom 06-15-2011 06:03 PM

[quote=arty-fishl;37504]
yes, and read them. (they don't have to end in .txt, but they should be plain text files ie. Not images etc.)[/quote]
Hmmm...
...
...
...
...
...
...
...
...
... :D:D:D:D:D

EDIT: It's not working. It creates new files all right but when I try to make it write a string variable to line 1 of the created file, it gives me an error.
[quote]Error during simulation.




Alice version: 2.2 02/10/2011



Throwable that caused the error:

Traceback (innermost last):

File "edu.cmu.cs.stage3.alice.core.property.ScriptProperty[name=script,owner=edu.cmu.cs.stage3.alice.core.response.ScriptResponse[world.iOTool.write_line.__Unnamed0__]]", line 1, in ?

File "<string>", line 7, in __init__

AttributeError: class 'org.python.modules.os' has no attribute 'path'


[/quote]

Mr Kidnapper 06-15-2011 06:33 PM

Mr. Kidnapper does not understand the uses of this. Is it supposed to assist in creating save/load states?

TauTrumpsPi 06-15-2011 07:06 PM

That is the reason I originally asked for it, yes.


All times are GMT -5. The time now is 01:19 AM.

Copyright ©2017, Carnegie Mellon University
Alice 2.x 1999-2012, Alice 3.x 2008-2012, Carnegie Mellon University. All rights reserved.