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 -- 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
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 --
"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
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
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
>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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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...
|
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 ?
|
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)
| |
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;
|
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...
|
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.
|
by: Edwin.Madari |
last post by:
use songs.extend( asongs ) #append is for single item - where ever it mightbe.
good luck.
Edwin
-----Original Message-----
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
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...
| |