os.walk question
How would one make a list of the files in the top directory using os.walk. I need to pick a random file from said list. Thanks. -- Posted on news://freenews.netfront.net - Complaints to [EMAIL PROTECTED] -- -- http://mail.python.org/mailman/listinfo/python-list
Raw Strings (I Think)
I've used glob.glob to get a list of files in a directory and now I want to use os.system to execute one of those files, the problem is that python automatically puts a escape charater infront of the back slashes so the os.system gets X:\\\\\\ and is useless, I think I need to convert my string to a raw string but I don't know how. -- Posted on news://freenews.netfront.net - Complaints to [EMAIL PROTECTED] -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Raw Strings (I Think)
> No, it doesn't. Instead of guessing what the cause might be, please show
> us your code and show us the error message you're getting, so that we can
> determine what the cause really is.
Ok, sorry. Heres my code:
import glob
import random
import os
songs = glob.glob('C:\###\###\###\*.mp3')
pick = random.choice(songs)
os.system(pick)
And yes, I know there are better ways of randomly selecting
a .mp3 file to play but I don't care.
-- Posted on news://freenews.netfront.net - Complaints to [EMAIL PROTECTED] --
--
http://mail.python.org/mailman/listinfo/python-list
Re: Raw Strings (I Think)
> my guess is that the real problem is that you get back filenames with
> spaces in them, which gets treated as multiple arguments by os.system.
>
> using os.startfile will fix this:
>
> >>> import glob, os, random
> >>> file = random.choice(glob.glob("\\music\\*.mp3"))
> >>> file
> '\\music\\Madrugada - Grit - 05 - Seven Seconds.mp3'
> >>> print file
> \music\Madrugada - Grit - 05 - Seven Seconds.mp3
> >>> os.system(file)
> '\music\Madrugada' is not recognized as an internal or external command,
> operable program or batch file.
> 1
> >>> os.startfile(file)
> ... music starts playing ...
>
>
Thanks I just switched the startfile for system and it worked like a charm
Thanks
-- Posted on news://freenews.netfront.net - Complaints to [EMAIL PROTECTED] --
--
http://mail.python.org/mailman/listinfo/python-list
Random Problems
Well the othe day I was making a program to make a list of all the songs in
certian directorys but I got a problem, only one of the directorys was added
to the list. Heres my code:
import random
import os
import glob
songs = glob.glob('C:\Documents and Settings\Admin\My
Documents\LimeWire\Saved\*.mp3')
asongs = glob.glob('C:\Documents and Settings\Admin\My
Documents\Downloads\*\*.mp3')
songs.append(asongs)
asongs = glob.glob('C:\Documents and Settings\Admin\My
Documents\Downloads\*\*\*.mp3')
songs.append(asongs)
asongs = glob.glob('C:\Documents and Settings\Admin\My
Documents\Downloads\*\*\*\*.mp3')
songs.append(asongs)
pick = random.choice(songs)
all goes well but pick awalys is from the first directory but songs awalys
includes all the files I want it to. Im baffaled.
-- Posted on news://freenews.netfront.net - Complaints to [EMAIL PROTECTED] --
--
http://mail.python.org/mailman/listinfo/python-list
Re: Random Problems
> 1) You need to either use raw string for your pathnames or use forward
> slashes.
> This is because backslash is an escape character to Python and if you get
> any legal escaped sequence (like \n, \t, etc) it won't work as expected.
>
> songs = glob.glob(r'C:\Documents and Settings\Admin\My
> Documents\LimeWire\Saved\*.mp3')
>
> or
>
> songs = glob.glob('C:/Documents and Settings/Admin/My
> Documents/LimeWire/Saved/*.mp3')
>
> Yes, forward slashes work just fine on windows.
>
> 2) When you have a list (songs) and append another list (asongs) you don't
> get a combined list, you get a list with the last element being the second
> list.
>
> example:
>
> >>> songs = [1,2,3]
> >>> asongs = [4,5,6]
> >>> songs.append(asongs)
> >>> songs
> [1, 2, 3, [4, 5, 6]]
> >>>
>
> What you wanted was songs.extend(asongs). BTW-Inserting a couple of print
> statements would have shown you this problem pretty quickly.
>
> -Larry
>
Thanks for the speedy responce, it really helped
-- Posted on news://freenews.netfront.net - Complaints to [EMAIL PROTECTED] --
--
http://mail.python.org/mailman/listinfo/python-list
indices question
pretty self-explanatory, here's what I put in: while stat == 0 : pgrid#ignore, A pre-defined function print "what cell do you want?" varcc = raw_input grid[varc] = 'O' And here's what I get back: Traceback (most recent call last): File "C:\py_prog\Tic Tac Toe.py", line 27, in grid[varc] = 'O' TypeError: list indices must be integers Please don't tell me that "list indices must be integers" because I know that, Why can't I put a varible thats an integer instead? -- Posted on news://freenews.netfront.net - Complaints to [EMAIL PROTECTED] -- -- http://mail.python.org/mailman/listinfo/python-list
Re: indices question
> Lanny: >> ... >> varcc = raw_input >> grid[varc] = 'O' >> ... >> Why can't I put a varible thats an integer instead? > > 'varcc' and 'varc' are different names. > > 'raw_input' isn't a function call, you may want to turn that into a > function call. > > varc isn't an integer, you may have to convert it to integer first, > using int(), because Python lists aren't like Lua ones, they are > represented with a sequence of cells, and an integer number is used to > denote what cells you want. Note that the cell must already exists > before accessing its contents. > > Maybe you want to use an associative array instead, named 'dict' in > Python, that allows you freedom in the type of the keys and allows you > to create cells on the fly. > > Surely my answer isn't enough to solve your problems, but it may give > you a starting point. > > Bye, > bearophile Thanks, this was very usefull -- Posted on news://freenews.netfront.net - Complaints to [EMAIL PROTECTED] -- -- http://mail.python.org/mailman/listinfo/python-list
Python RPG Codebase
I've been thinking about putting together a text based RPG written fully in Python, possibly expanding to a MUD system. I'd like to know if anyone feels any kind of need for this thing or if I'd be wasting my time, and also if anyone would be interested in participating, because of the highly modular nature of any RPG it should make for easy cooperation. -- http://mail.python.org/mailman/listinfo/python-list
Help (I can't think of a better title)
The answer may be right infront of me but I really can't figure this
out.
I'm trying to build a interactive fiction kind of game, silly I know
but I
am a fan of the genre. I'm trying to build up an index of all the
rooms in
the game from an outside file called roomlist.txt. The only problem is
that
every room ends up having four exits. Here's the code.
class room() :
room_id = 'room_id'
name = 'room'
description = 'description'
item_list =
exits = {}
visits = 0
def leave(self, direction)
global roomlist
global player_current_room
if direction not in player_current_room.exitskeys() :
print 'There is no exit in that direction.'
return 1
roomlist[self.room_id] = self
player_current_room =
roomlist[player_current_room.exits[direction]]
print player_current_room.name
if player_current_room.visits < 1 :
print player_current_room.description
if player_current_room.item_list != [] :
stdout.write('You can see ')
for item in player_current_room.item_list :
stdout.write('a')
if item.display_name[0] in ['a','e','i','o','u'] :
stdout.write('n ' + item.display_name
+
',')
else :
stdout.write(item.display_name + ',')
pass
print
print 'Exits:',
for way in player_current_room.exits :
print way.capitalize(),
print
player_current_room.visits += 1
pass
else :
player_current_room.visits += 1
pass
pass
def build_rooms(room_file) :
global roomlist
rfile = open(room_file)
tmp_builder = ''
for line in rfile :
tmp_builder = tmp_builder + line[:-1]
pass
for location in tmp_builder.rsplit('::') :
if location.rsplit(';')[-1] == '' :
location = location[:-1]
if len(location.rsplit(';')) != 5 :
if '-dev' or '-v' in argv :
print location.rsplit(';')[0], 'had',
len(location.rsplit(';')), 'values in it, 5 expected'
for value in location.rsplit(';') :
print; print value
foobar.append(value)
print 'A room failed to initalize due to either too
much or
not enough values being present in the build file'
pass
pass
else :
roomlist[location.rsplit(';')[0]] = room()
roomlist[location.rsplit(';')[0]].room_id =
location.rsplit(';')[0]
roomlist[location.rsplit(';')[0]].name =
location.rsplit(';')[1]
roomlist[location.rsplit(';')[0]].description =
location.rsplit(';')[2]
if location.rsplit(';')[3] != 'none' :
pass
tmp_var = location.rsplit(';')[4]
print location.rsplit(';')[0],
roomlist[location.rsplit(';')[0]].exits, 'before'
for way in tmp_var.rsplit(',') :
roomlist[location.rsplit(';')[0]].exits[way.rsplit(':')
[0]]
= way.rsplit(':')[1]
roomlist = {}
build_rooms('room_list.txt')
And here is the roomlist.txt file :
start_room;
Starting Room;
This is the starting room, if you can read this text it means that at
least
one part of this beta is working.;
none;
north:second_room,west:aux_room;
::
second_room;
Second Room;
Yo, Yo! This is the second room, if you can see this text a
substantitally
greater amount of the game is running than would have been if you
didn't see
this text.;
apple;
south:start_room;
::
aux_room;
Auxillary Room;
No, there aren't any barbarian conscripts here, but there is a table!;
none;
east:start_room;
Ideally roomlist['start_room'].exits would equal {'aux_room' :
'west',
'second_room' : 'north'} but it doesn't. Sorry if this is unclear or
too
long, but I'm really stumped why it is giving bad output
--
http://mail.python.org/mailman/listinfo/python-list
