So I took a class on python but I haven't used it in a while so I'm a bit rusty. I was just messing around with it and I got to wondering how do you refer to other files from your program. By that I mean say my program uses a list, but the list is really big so it's its own file, how would I go about using that file in my program. I'm assuming this is possible because I know you can read and edit text file from within python. I'm using 3.1.1 btw. any help is appreciated. thanks.
Just to let you know, Python 2.x and 3.x are very different, so use whatever your class uses.
[QUOTE=CANAD14N;20629454]So I took a class on python but I haven't used it in a while so I'm a bit rusty. I was just messing around with it and I got to wondering how do you refer to other files from your program. By that I mean say my program uses a list, but the list is really big so it's its own file, how would I go about using that file in my program. I'm assuming this is possible because I know you can read and edit text file from within python. I'm using 3.1.1 btw. any help is appreciated. thanks.[/QUOTE]
Ok, there are several things this could mean, and several ways of achieving them.
The simplest thing would be to have a file called something like the_list_is_here.py and have the code of that being along the lines of:
[code]list = ['element one', 'element two', 'SPAM', ...][/code]
And then importing it as a module.
Secondly, you could have a standard plain text file. If we assume the file is a plaintext document with comma separated members, we could do
[code]with open('list.txt') as file:
list = file.read().split(',')[/code]
And you could, if you are saving a list directly from the program, use one of the marshal, pickle, shelve or dbm modules, although anything other than perhaps marshal would just be huge overkill.
[code]require list[/code]
assuming list.py is your massive giant list
[QUOTE=TheKnife;20636423][code]require list[/code]
assuming list.py is your massive giant list[/QUOTE]
I think you mean
[code]
import list
[/code]
Whoops yes I do
I mixed it up with ruby
[QUOTE=TheBoff;20629775]Ok, there are several things this could mean, and several ways of achieving them.
The simplest thing would be to have a file called something like the_list_is_here.py and have the code of that being along the lines of:
[code]list = ['element one', 'element two', 'SPAM', ...][/code]
And then importing it as a module.
Secondly, you could have a standard plain text file. If we assume the file is a plaintext document with comma separated members, we could do
[code]with open('list.txt') as file:
list = file.read().split(',')[/code]
And you could, if you are saving a list directly from the program, use one of the marshal, pickle, shelve or dbm modules, although anything other than perhaps marshal would just be huge overkill.[/QUOTE]
Going with that last note of saving a list directly from the program, wouldn't you be able to pickle it then use bz2 to compress it? it'd add more cpu usage but should cut back on the size, but that's really only necessary if he's talking about a list that's like... 20 MB.
[QUOTE=<ToD> Aaron;20692719]Going with that last note of saving a list directly from the program, wouldn't you be able to pickle it then use bz2 to compress it? it'd add more cpu usage but should cut back on the size, but that's really only necessary if he's talking about a list that's like... 20 MB.[/QUOTE]
The problem with that is that it's not possible to hand edit the file, unless you learn the pickle format, which is why I mentioned it in passing, rather than gave an example. It is possible though, of course.
Sorry, you need to Log In to post a reply to this thread.