473,804 Members | 3,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:\D ocuments and Settings\Admin\ My
Documents\LimeW ire\Saved\*.mp3 ')
asongs = glob.glob('C:\D ocuments and Settings\Admin\ My
Documents\Downl oads\*\*.mp3')
songs.append(as ongs)
asongs = glob.glob('C:\D ocuments and Settings\Admin\ My
Documents\Downl oads\*\*\*.mp3' )
songs.append(as ongs)
asongs = glob.glob('C:\D ocuments and Settings\Admin\ My
Documents\Downl oads\*\*\*\*.mp 3')
songs.append(as ongs)
pick = random.choice(s ongs)

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 ne**@netfront.n et --
Aug 13 '08 #1
6 1510
Lanny wrote:
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:\D ocuments and Settings\Admin\ My
Documents\LimeW ire\Saved\*.mp3 ')
asongs = glob.glob('C:\D ocuments and Settings\Admin\ My
Documents\Downl oads\*\*.mp3')
songs.append(as ongs)
asongs = glob.glob('C:\D ocuments and Settings\Admin\ My
Documents\Downl oads\*\*\*.mp3' )
songs.append(as ongs)
asongs = glob.glob('C:\D ocuments and Settings\Admin\ My
Documents\Downl oads\*\*\*\*.mp 3')
songs.append(as ongs)
pick = random.choice(s ongs)

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 ne**@netfront.n et --
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\LimeW ire\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(as ongs). BTW-Inserting a couple of print
statements would have shown you this problem pretty quickly.

-Larry

Aug 13 '08 #2
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\LimeW ire\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(a songs)
songs
[1, 2, 3, [4, 5, 6]]
>>

What you wanted was songs.extend(as ongs). 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 ne**@netfront.n et --
Aug 13 '08 #3
"Lanny" <la***@freshell s.chwrites:
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
If you need recursive traversal of directories, try os.walk. For
example:

mp3s = []
for root, subdirs, files in os.walk(r'C:\Do cuments and Settings\Admin\ My Documents\Downl oads'):
for f in files:
if f.endswith('.mp 3'):
mp3s.append(os. path.join(root, f))
# mp3s is now a list of mp3 files under
# C:\Documents and Settings\Admin\ My Documents\Downl oads
Aug 13 '08 #4

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.
....
Here's some code .... that illustrates yours ....

import glob

songs = glob.glob( '/path/to/somewhere/*.mp3' )

asongs = glob.glob( 'path/to/somewhere/else/*.mp3' )

songs.append( asongs )

# repeat a few times appending lists from other dirs
all goes well but pick awalys is from the first directory
but songs awalys includes all the files I want it to.
songs.append( asongs ) is appending the entire asongs list
as a single item to the end of the songs list, not adding
each individual song as an entry ....

For example ....
>>l1 = range( 0 , 5 )
l2 = range( 5 , 10 )
l3 = range( 11 , 15 )

l1
[0, 1, 2, 3, 4]
>>>
l2
[5, 6, 7, 8, 9]
>>>
l3
[11, 12, 13, 14]
>>>
l1.append( l2 )

l1
[0, 1, 2, 3, 4, [5, 6, 7, 8, 9]]
>>>
l1.append( l3 )

l1
[0, 1, 2, 3, 4, [5, 6, 7, 8, 9], [11, 12, 13, 14]]

So, if you have a lot of entries in the original songs list
you're only adding a few entries to it in the form of another
list and most likely you didn't run enough random.choice tests
to flush out a pick that turned out to be one of the entire
asong lists that you added ....

You might try something like the following
where each tune gets added individually to
the song pool .... un-tested ....
# -------------------------------------------------------------------

import random
import glob

base_dir = 'c:/Documents and Settings/Admin/My Documents'

list_subdirs = [
'LimeWire/Saved/*.mp3' ,
'Downloads/*/*.mp3' ,
'Downloads/*/*/*.mp3' ,
'Downloads/*/*/*/*.mp3 ]

song_pool = [ ]

for this_dir in list_subdirs :

list_songs = glob.glob( "'%s/%s'" % ( base_dir , this_dir )

if list_songs :

for this_song in list_songs :

song_pool.appen d( this_song )

npicks = 41

print

for n in range( npicks ) :

this_pick = random.choice( song_pool )

print ' ' , this_pick

# -------------------------------------------------------------------
--
Stanley C. Kitching
Human Being
Phoenix, Arizona

Aug 14 '08 #5
list_songs = glob.glob( "'%s/%s'" % ( base_dir , this_dir )
Missed a closing paren ....

list_songs = glob.glob( "'%s/%s'" % ( base_dir , this_dir ) )

Still .... NOT Tested
--
Stanley C. Kitching
Human Being
Phoenix, Arizona

Aug 14 '08 #6
>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:\D ocuments and Settings\Admin\ My
Documents\Lime Wire\Saved\*.mp 3')

asongs = glob.glob('C:\D ocuments and Settings\Admin\ My
From: Ed**********@ve rizonwireless.c om
To: "Cousin Stanley" <co***********@ gmail.com>,
py*********@pyt hon.org
Date: Yesterday 07:28:18 pm
use songs.extend( asongs ) # append is for single item
# - where ever it might be.
>>l1 = range(5) * * * *
l2 = range(5,10)
l1
[0, 1, 2, 3, 4]
>>l2
[5, 6, 7, 8, 9]
>>l1.extend(l 2)
l1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
And ....

Thanks to Edwin Madari for reminding me about
the extend method available for lists which
I had totally forgotten about ....
--
Stanley C. Kitching
Human Being
Phoenix, Arizona

Aug 14 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3269
by: Dr John Stockton | last post by:
Q1 : Given an array such as might have been generated by var A = is there a highly effective way of reducing it to - i.e. removing the undefineds and shifting the rest down? A.sort().slice(0,n) // would do it, but sorts; and the number
8
1907
by: jon morgan | last post by:
OK, I'm going to be brave. There is a bug in VS.Net 1.1 that causes random compiler errors. I have raised this issue in posts at least three time in the past couple of months without attracting much interest. But it's driving me nuts. Here's what happens. I'm working on a multi project VB app. happily writing nice inoffensive code - go to compile and the compiler tells me there's a problem in a project I'm not working on. But really...
10
2911
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I have is that the first loop I get a sequence of random numbers untuil I get a match, BUT then on the following loops I get the SAME random(?) sequence. I am using rand(). I do not want to get too fancy with the random number generator, but is there...
9
3856
by: greeningster | last post by:
I have written an application in Visual C++ for a customer but it seems to crash randomly. Could anyone give me any help on how I could track this down ? Also, there appears there might be memory leaks too. How can i track these down ?
6
4692
by: Starbuck | last post by:
Hi In VB6 we used the following to create a unique random number - Function longSerial() As Long longSerial = Val((Format$(Int(Rnd * 424) - 212)) + Format$((Timer * 100), "0000000")) longSerial = longSerial Xor Int(2147483647 * Rnd)
13
3622
by: Roy Gourgi | last post by:
Hi, How do I invoke the random number generator that was suggested by a few people. Ideally, what I would like to do is to instantiate the random no. generator with a seed value that does not repeat the values and that can be called from any class, as I have to call the random number generator from a few different classes. Here is my code: using System;
5
3355
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible double value in the range could come up with equal probability). I'd also like to be able to seed this generator (e.g., via the clock) so that the same sequence of random values don't come up every time. Anybody have an easy and fast...
2
1814
by: blaine | last post by:
Hey everyone, Just a friendly question about an efficient way to do this. I have a graph with nodes and edges (networkx is am amazing library, check it out!). I also have a lookup table with weights of each edge. So: weights = .12 weights = .53 weights = 1.23 weights = -2.34 etc.
0
989
by: Edwin.Madari | last post by:
use songs.extend( asongs ) #append is for single item - where ever it mightbe. good luck. Edwin -----Original Message-----
20
2304
by: Robbie Hatley | last post by:
I needed a quick program called "random" that gives a random positive integer from n1 to n2. For example, if I type "random 38, 43", I want the program to print a random member of the set {38, 39, 40, 41, 42, 43}. Also, I read in my compiler's documentation the following: To get a random number in the range 0..N, use rand()%(N+1). Note that the low bits of the rand's return value are not very random, so rand()%N for small values of N...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.